国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#使用SqlDataAdapter.Update更新數據庫

2019-11-17 04:10:37
字體:
來源:轉載
供稿:網友

此處將資料從數據庫得出后顯在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"];
        }
    }
}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 开封市| 舒兰市| 黔江区| 大石桥市| 河津市| 深水埗区| 共和县| 海晏县| 新宁县| 罗城| 大英县| 木兰县| 甘孜县| 苗栗市| 惠来县| 锡林浩特市| 嫩江县| 永城市| 浠水县| 布尔津县| 开阳县| 旬阳县| 溧水县| 定结县| 龙井市| 北川| 疏附县| 安平县| 浦城县| 东方市| 麻栗坡县| 绍兴市| 锡林郭勒盟| 乐清市| 丹阳市| 乐昌市| 壤塘县| 日土县| 安多县| 应城市| 西盟|