C#与sql server数据库存储过程的操作实例
Posted heroysj
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#与sql server数据库存储过程的操作实例相关的知识,希望对你有一定的参考价值。
通过这几天的学习和实际操作,把C#与sql server数据库存储过程的操作搞清楚了。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace sqltest1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delData(textBox1.Text);
}
private void delData(string v)
{
SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "deldata";
cmd.CommandType = CommandType.StoredProcedure;
//SqlParameter[] sps = new SqlParameter[] { new SqlParameter("@id",v) };
cmd.Parameters.Add(new SqlParameter("@id", v));
int i = cmd.ExecuteNonQuery();
MessageBox.Show($"有{i}条数据受到影响!");
}
private void button2_Click(object sender, EventArgs e)
{
AddData(textBox2.Text, textBox3.Text);
}
private void AddData(string text1, string text2)
{
SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000");
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "AddData";
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter[] sps = new SqlParameter[] {
new SqlParameter("@test1",text1),
new SqlParameter("@test2",text2)
};
cmd.Parameters.AddRange(sps);
int i = cmd.ExecuteNonQuery();
MessageBox.Show($"有{i}条数据受到影响!");
}
}
}
以上是关于C#与sql server数据库存储过程的操作实例的主要内容,如果未能解决你的问题,请参考以下文章