hollow,欢迎来到爱不绝迹的博客园!

SqlDataAdapter 对datagridview进行增删改(A)

这种方法主要是双击datagridview单元格,直接进行添加,修改,删除,在实际开发中并不太常用,另一种方法下一次在具体陈述。

 1 using System;
 2 using System.Collections.Generic;
 3 using System.ComponentModel;
 4 using System.Data;
 5 using System.Drawing;
 6 using System.Linq;
 7 using System.Text;
 8 using System.Windows.Forms;
 9 using System.Data;
10 using System.Data.SqlClient;
引用

 

 

  1. namespace SQlCommandBuilter
  2. {
  3.     public partial class Form1 : Form
  4.     {
  5.         public Form1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.         //连接数据库字符串
  10.         private static  readonly string Constring = "server=.;database=stu;uid=sa;pwd=fiybird";
  11.         SqlConnection con = new SqlConnection(Form1.Constring);//创建SqlConnection对象
  12.         DataTable dt=new DataTable();//创建dataTable
  13.         private SqlDataAdapter apt = null;//声明一个SqlDataAdapter对象
  14.         private void Bind()
  15.         {
  16.             string sql = string.Format("select id,no,name,age,gender,address from stuinfo");
  17.             apt = new SqlDataAdapter(sql, con);//实例化SqlDataAdapter
  18.             apt.Fill(dt);//填充数据
  19.             //SqlCommandBuilder 必不可少
  20.             /*
  21.             为了生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 会自动使用 SelectCommand 属性来检索所需的元数据集。
  22.             如果在检索到元数据后(例如在第一次更新后)更改 SelectCommand,则应调用 RefreshSchema 方法来更新元数据。
  23.             SelectCommand 还必须至少返回一个主键列或唯一的列。如果什么都没有返回,就会产生 InvalidOperation 异常,不生成命令。
  24.             SqlCommandBuilder 还使用由 SelectCommand 引用的 Connection、CommandTimeout 和 Transaction 属性。
  25.             如果修改了这些属性中的一个或多个,或者替换了 SelectCommand 本身,用户则应调用 RefreshSchema。
  26.             否则,InsertCommand、UpdateCommand 和 DeleteCommand 属性都保留它们以前的值。
  27.             如果调用 Dispose,则会解除 SqlCommandBuilder 与 SqlDataAdapter 的关联,并且不再使用生成的命令。
  28.              */
  29.             SqlCommandBuilder sbBuilder=new SqlCommandBuilder(apt);
  30.             this.dataGridView1.AutoGenerateColumns = false;//不自动生成列
  31.             this.dataGridView1.DataSource = dt;
  32.         }
  33.         private void Form1_Load(object sender, EventArgs e)
  34.         {
  35.            Bind();
  36.         }
  37.         private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
  38.         {
  39.             apt.Update(dt);
  40.             MessageBox.Show("保存成功!");
  41.         }
  42.         private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
  43.         {
  44.              dt.Clear();//清空dataTable当前数据
  45.             apt.Fill(dt);//重新填充数据
  46.         }
  47.         private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
  48.         {
  49.             //是否选中一行数据
  50.             if (this.dataGridView1.CurrentRow.Selected)
  51.             {
  52.                 DialogResult dr = MessageBox.Show("确定要删除么?", "友好提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
  53.                 if (dr == DialogResult.Yes)
  54.                 {
  55.                     this.dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
  56.                     new SqlCommandBuilder(apt);
  57.                     apt.Update(dt);
  58.                     MessageBox.Show("删除成功!");
  59.                 }
  60.             }
  61.             else
  62.             {
  63.                 MessageBox.Show("请先选中一行数据,然后在进行删除!");
  64.                 return;
  65.             }
  66.         }
  67.     }
  68. }
posted @ 2015-06-01 17:47  爱不绝迹  阅读(1557)  评论(0编辑  收藏  举报