开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 8146|回复: 0

SqlCommandBuilder类和SqlDataAdapter.Update()方法。

[复制链接]

0

精华

5

贡献

16

赞扬

帖子
20
软币
212
在线时间
31 小时
注册时间
2013-10-25
发表于 2013-11-8 10:58:22 | 显示全部楼层 |阅读模式

此处将资料从数据库得出后显在DataGridView上显示。然后变更数据。变更数据的方法在按钮事件中。

    资料显示方法不用多说。

    更新时主要用到SqlCommandBuilder类和SqlDataAdapter.Update()方法。

        SqlCommandBuilder对象负责生成用于更新数据库的SQL语句,不必自己创建这些语句。

        UpDate方法自动遍历DataTable中的行,以找出需要对数据库作出变动。Rows集合中每个DataRow对象都具有属性RowState,可以跟踪此行是否已删除、添加、修改,还是未作变动。所作的任何变化都会反映到数据库中。

    例:更新表中内容:

using System.Data.SqlClient;

namespace UpdatingData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
            UpdateData();
        }


        string sConnection = "Data Source=scott;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;PassWord=sa123";
        DataSet dsSet = new DataSet();
        SqlDataAdapter sdaAdapter = null;
        SqlCommandBuilder scbBuilder = null;
        PRivate void UpdateData()
        {
            //建立Connection
            SqlConnection scConnection = new SqlConnection(sConnection);
            //建立Command
            SqlCommand scCommand = scConnection.CreateCommand();
            scCommand.CommandText = "select customerID,contactName from customers";
            //建立Adapter
            sdaAdapter = new SqlDataAdapter(scCommand);

            //该对象负责生成用于更新数据库的SQL语句,不必自己创建这些语句
            scbBuilder = new SqlCommandBuilder(sdaAdapter);

            //得到数据
            sdaAdapter.Fill(dsSet, "customers");
            dgvView.DataSource = dsSet.Tables["customers"];

        }

        void btnUpdate_Click(object sender, EventArgs e)
        {
            //设置值
            dsSet.Tables["customers"].Rows[3]["contactName"] = "Thomas Hardy";
            //更新数据(UpDate方法自动遍历DataTable中的行,以找出需要对数据库作出变动)
            //Rows集合中每个DataRow对象都具有属性RowState,可以跟踪此行是否已删除、添加、修改,还是未作变动。所作的任何变化都会反映到数据库中。
            sdaAdapter.Update(dsSet, "customers");
            dgvView.DataSource = dsSet.Tables["customers"];

        }
    }
}
例:在表中增加行

using System.Data.SqlClient;

namespace AddingData
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            btnAdd.Click += new EventHandler(btnAdd_Click);

            UpdateData();
        }


        string sConnection = "Data Source=scott;Initial Catalog=northwind;Persist Security Info=True;User ID=sa;Password=sa123";
        DataSet dsSet = new DataSet();
        SqlDataAdapter sdaAdapter = null;
        SqlCommandBuilder scbBuilder = null;
        private void UpdateData()
        {
            //建立Connection
            SqlConnection scConnection = new SqlConnection(sConnection);
            //建立Command
            SqlCommand scCommand = scConnection.CreateCommand();
            scCommand.CommandText = "select customerID,companyName from customers";
            //建立Adapter
            sdaAdapter = new SqlDataAdapter(scCommand);

            //该对象负责生成用于更新数据库的SQL语句,不必自己创建这些语句
            scbBuilder = new SqlCommandBuilder(sdaAdapter);

            //得到数据
            sdaAdapter.Fill(dsSet, "customers");
            dgvView.DataSource = dsSet.Tables["customers"];
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            AddRow();
        }
        private void AddRow()
        {
            //新建表中的行
            DataRow drRow = dsSet.Tables["customers"].NewRow();
            drRow["customerID"] = "ZaCzi";
            drRow["companyName"] = "Zachary Zithers Ltd.";
            //增加行
            dsSet.Tables["customers"].Rows.Add(drRow);

            //更新表
            sdaAdapter.Update(dsSet, "customers");
            
            //显示
            dgvView.DataSource = dsSet.Tables["customers"];
        }
    }
}


回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-3-29 18:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表