1. 程式人生 > >DataTable修改後,如何更新資料庫

DataTable修改後,如何更新資料庫

       DataTable中的資料可以修改多個後,同時更新到資料庫,這樣比較方便。下面是實現的程式碼:

            DataTable table = new DataTable();//初始化一個DataTable物件
            
            string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;";
            SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//連線資料庫
            
            SqlCommand sqlCommand = new SqlCommand("select * from testB", sqlConnection);
            
            SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand);
            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//這句挺重要的,好像是批量更新的時候用
            
            //DataSet dtst = new DataSet();      用dataSet也可以完成,下面有對應實現的程式碼

            sqlConnection.Open();
            sqlAdap.Fill(table);
       
            //table = dtst.Tables["testB"];

            DataRow dr = table.Rows[0];//第一行
            
            //dr.BeginEdit();
            //dr.EndEdit();     貌似這兩句主要是資料繫結到控制元件的時候才用

            for (int i = 0; i < table.Rows.Count; i++) //修改table中每一行的內容
            {
                dr = table.Rows[i];
                dr["f"] = 3;       //欄位名為f
             }

            //testB表中必須存在主鍵,否則無法更新// ****** 這個很重要 *****
            sqlAdap.Update(table);
            //sqlAdap.Update(dtst, "testB");

            sqlConnection.Close();
            MessageBox.Show("aa");

       下面是DataTable與dataGridView   控制元件繫結,當dataGridView中的資料改變時,更新資料庫的程式碼:

             DataTable和dataGridView控制元件繫結程式碼:

private void Form1_Load(object sender, EventArgs e)
        {
            string sqlConnectionString = "Data Source=ComputerName-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;";
            SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);//初始化sqlconnection

            DataTable table = new DataTable();
            SqlDataAdapter sqlAdap = new SqlDataAdapter("select * from testB ", sqlConnection);

            sqlConnection.Open();
            sqlAdap.Fill(table);
            sqlConnection.Close();

            //將testB表中的資料顯示在dataGridView中
            this.dataGridView1.DataSource = table;

        }

           實現將dataGridView中的改動更新到資料庫程式碼:

private void butUpdate_Click(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            table = (DataTable)this.dataGridView1.DataSource;

            string sqlConnectionString = "Data Source=WangHaitao-PC;Initial Catalog=charge_sys;User ID=sa;PWD=123456;";
            SqlConnection sqlConnection = new SqlConnection(sqlConnectionString);
            
            SqlCommand sqlCommand = new SqlCommand("select * from testB", sqlConnection);
            
            SqlDataAdapter sqlAdap = new SqlDataAdapter(sqlCommand);
            SqlCommandBuilder sqlBuilder = new SqlCommandBuilder(sqlAdap);//必須有

            sqlConnection.Open();
            sqlAdap.Fill(table);

            //testB表中必須存在主鍵,否則無法更新
            sqlAdap.Update(table);

            sqlConnection.Close();
            MessageBox.Show("aa");

        }