Sql server 实现增删改查


[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1.  using System.Data.SqlClient;  
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. SqlConnection conn;  
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //连接数据库  
  2. private void Form1_Load(object sender, EventArgs e)  
  3. {  
  4.     string constr = "server=ACER-PC\\LI;database=db_test;uid=sa;pwd=123";  
  5.     conn = new SqlConnection(constr);  //数据库连接     
  6. }  
  7.   
  8. //查看数据库信息  
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //这里只要连接数据库即可,不必打开数据库  
  2. private void button1_Click(object sender, EventArgs e)  
  3. {  
  4.     SqlCommand cmd = new SqlCommand("select * from tb_ls", conn);  
  5.   
  6.     SqlDataAdapter sda = new SqlDataAdapter();  
  7.     sda.SelectCommand = cmd;  
  8.   
  9.     DataSet ds = new DataSet();  
  10.   
  11.     sda.Fill(ds, "cs");  
  12.   
  13.     dataGridView1.DataSource = ds.Tables[0];  
  14. }  
  15.   
  16. //删除选中的一行信息  
  17. private void button2_Click(object sender, EventArgs e)  
  18. {  
  19.     if (this.dataGridView1.SelectedRows.Count > 0)  
  20.     {  
  21.         DataRowView drv = dataGridView1.SelectedRows[0].DataBoundItem as DataRowView;  
  22.         drv.Delete();  
  23.     }  
  24.     conn.Open();//打开数据库  
  25.     SqlCommand cmd = new SqlCommand("delete from tb_ls where 编号="+this.dataGridView1.CurrentRow.Cells["编号"].Value+"",conn);  
  26.     cmd.ExecuteNonQuery();  
  27.     conn.Close();//关闭数据库  
  28. }  
  29.   
  30. //添加信息  
  31. private void button3_Click(object sender, EventArgs e)  
  32. {  
  33.     conn.Open();  
  34.     SqlCommand cmd = new SqlCommand("insert into tb_ls values('"+textBox1.Text+"','"+textBox2.Text+"','"+textBox3.Text+"','"+textBox4.Text+"')",conn);  
  35.     cmd.ExecuteNonQuery();  
  36.     conn.Close();  
  37. }  
  38.   
  39. private void button4_Click(object sender, EventArgs e)  
  40. {  
  41.     conn.Open();  
  42.     SqlCommand cmd = new SqlCommand("update tb_ls set 姓名='"+textBox2.Text+"',性别='"+textBox3.Text+"',年龄='"+textBox4.Text+"' where 编号='"+textBox1.Text+"'",conn);  
  43.     textBox1.ReadOnly = false;  
  44.     cmd.ExecuteNonQuery();  
  45.     conn.Close();  
  46. }  
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. //将选中的某一行的信息显示在TextBox文本框中  
  2.  private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)  
  3.  {  
  4.      textBox1.ReadOnly = true;  
  5.      textBox1.Text = this.dataGridView1.CurrentRow.Cells["编号"].Value.ToString();  
  6.      textBox2.Text = this.dataGridView1.CurrentRow.Cells["姓名"].Value.ToString();  
  7.      textBox3.Text = this.dataGridView1.CurrentRow.Cells["性别"].Value.ToString();  
  8.      textBox4.Text = this.dataGridView1.CurrentRow.Cells["年龄"].Value.ToString();  
  9.  } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值