winForm中DataSet实现数据查询、保存和删除

来源:互联网 发布:淘宝新店推广技巧 编辑:程序博客网 时间:2024/06/06 11:45

    public partial class Form4 : Form    {        SqlConnection conn = null;        SqlDataAdapter adp = null;        DataSet ds = null;        string constr = "data source=.;initial catalog=myschool;user id=sa;pwd=sa";        string sql = "select * from grade";        public Form4()        {            InitializeComponent();            conn = new SqlConnection(constr);            adp = new SqlDataAdapter(sql, conn);            ds = new DataSet("grade");        }        #region ------------数据查询----------------        private void button1_Click(object sender, EventArgs e)        {                                  ds.Clear();            adp.Fill(ds, "grade");            dataGridView1.DataSource = ds.Tables["grade"];        }        #endregion        #region ------------数据保存与更新----------------        private void button2_Click(object sender, EventArgs e)        {            SqlCommandBuilder builder = new SqlCommandBuilder(adp);            adp.Update(ds,"grade");            updateGridView();        }        #endregion        #region----------数据删除-----------------        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)        {            SqlCommandBuilder builder = new SqlCommandBuilder(adp);            int rowIndex = dataGridView1.CurrentRow.Index;            //获取被选中行的首列内容            MessageBox.Show(ds.Tables["grade"].Rows[rowIndex][0].ToString());            ds.Tables["grade"].Rows[rowIndex].Delete();            adp.Update(ds, "grade");            updateGridView();        }        #endregion        #region ------------数据操作后的更新----------------        private void updateGridView()        {            ds.Clear();            adp.Fill(ds, "grade");            dataGridView1.DataSource = ds.Tables["grade"];        }        #endregion    }

SqlCommandBuilder将DataTable的数据更新回sql数据库中时,sql数据库中对应的表必须要有主键;

SqlDataAdapter不会自动生成实现 DataSet 的更改与关联的 SQL Server 实例之间的协调所需的 Transact-SQL 语句。但是,如果设置了 SqlDataAdapter 的SelectCommand 属性,则可以创建一个 SqlCommandBuilder 对象来自动生成用于单表更新的 Transact-SQL 语句。然后,SqlCommandBuilder 将生成其他任何未设置的 Transact-SQL 语句。
每当设置了 DataAdapter 属性,SqlCommandBuilder 就将其本身注册为 RowUpdating 事件的侦听器。一次只能将一个 SqlDataAdapter 与一个 SqlCommandBuilder 对象(或相反)互相关联。
为了生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 会自动使用 SelectCommand 属性来检索所需的元数据集。如果在检索到元数据后(例如在第一次更新后)更改 SelectCommand,则应调用 RefreshSchema 方法来更新元数据。
SelectCommand 还必须至少返回一个主键列或唯一的列。如果什么都没有返回,就会产生 InvalidOperation 异常,不生成命令。
SqlCommandBuilder 还使用由 SelectCommand 引用的 Connection、CommandTimeout 和 Transaction 属性。如果修改了这些属性中的一个或多个,或者替换了 SelectCommand 本身,用户则应调用 RefreshSchema。否则,InsertCommand、UpdateCommand 和 DeleteCommand 属性都保留它们以前的值。
如果调用 Dispose,则会解除 SqlCommandBuilder 与 SqlDataAdapter 的关联,并且不再使用生成的命令。

 

获取DataGridView中被选中的行中信息

private void tsmiAdd_Click(object sender, EventArgs e)
{
      MessageBox.Show(this.dgvStudent.SelectedRows[0].Cells[0].Value.ToString());
 }

也可以使用列表进行获取信息

 private void tmsiDeleteCom_Click(object sender, EventArgs e)
  {
            if (this.dgvStudent.SelectedRows.Count > 0)
            {               
                MessageBox.Show(this.dgvStudent.SelectedRows[0].Cells["studentNo"].Value);
            }
  }

需要在设置dgvStudent的设置对应的列明与DataPropertyName中的变量名一致,如下图:


原创粉丝点击