通过这几天的学习和实际操作,把C#与sql server数据库存储过程的操作搞清楚了。
1 using System; 2 using System.ComponentModel; 3 using System.Collections.Generic; 4 using System.Data; 5 using System.Data.SqlClient; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Windows.Forms; 10 11 namespace sqltest1 12 { 13 public partial class Form1 : Form 14 { 15 public Form1() 16 { 17 InitializeComponent(); 18 } 19 20 private void button1_Click(object sender, EventArgs e) 21 { 22 delData(textBox1.Text); 23 } 24 25 private void delData(string v) 26 { 27 SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000"); 28 conn.Open(); 29 SqlCommand cmd = conn.CreateCommand(); 30 cmd.CommandText = "deldata"; 31 cmd.CommandType = CommandType.StoredProcedure; 32 //SqlParameter[] sps = new SqlParameter[] { new SqlParameter("@id",v) }; 33 cmd.Parameters.Add(new SqlParameter("@id", v)); 34 int i = cmd.ExecuteNonQuery(); 35 MessageBox.Show($"有{i}条数据受到影响!"); 36 } 37 38 private void button2_Click(object sender, EventArgs e) 39 { 40 AddData(textBox2.Text, textBox3.Text); 41 } 42 43 private void AddData(string text1, string text2) 44 { 45 SqlConnection conn = new SqlConnection("data source = .; initial catalog = test; User ID = sa; password = Ly00000000"); 46 conn.Open(); 47 SqlCommand cmd = conn.CreateCommand(); 48 cmd.CommandText = "AddData"; 49 cmd.CommandType = CommandType.StoredProcedure; 50 SqlParameter[] sps = new SqlParameter[] { 51 new SqlParameter("@test1",text1), 52 new SqlParameter("@test2",text2) 53 }; 54 cmd.Parameters.AddRange(sps); 55 int i = cmd.ExecuteNonQuery(); 56 MessageBox.Show($"有{i}条数据受到影响!"); 57 } 58 } 59 }