日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#.net删除dataGridView中选中行并同时更新数据库

發布時間:2025/3/20 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#.net删除dataGridView中选中行并同时更新数据库 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
private void Delete_Click(object sender, EventArgs e)
??????? {

?????????????SqlConnection conn = new SqlConnection("數據庫連接串");
?????????????conn.Open();
?????????????SqlDataAdapter daAuthors = new SqlDataAdapter("Select * From student", conn);

?????????????DataSet dsPubs = new DataSet("Pubs");
?????????????daAuthors.FillSchema(dsPubs, SchemaType.Source, "student"); //FillSchema加載表的架構和數據,有了架構,表就知道哪個列是它的主鍵,同時 Rows 集合的 Find 方法也就可用了。


?????????????daAuthors.Fill(dsPubs, "student");
?????????????DataTable tblAuthors;
?????????????tblAuthors = dsPubs.Tables["student"];


??????????? if (MessageBox.Show("確實要刪除該行嗎?", "詢問", MessageBoxButtons.YesNo) == DialogResult.Yes)
??????????? {
????????????????DataRow drCurrent;??????????????
????????????????string Row_zhi = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();//獲取第一個單元格的值????????????
??????????????? //MessageBox.Show(Row_zhi);???????????
??????????????? drCurrent = tblAuthors.Rows.Find(Row_zhi);
????????????????drCurrent.Delete();

??????????????? SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(daAuthors); //SqlCommandBuilder 提供自動生成單表命令的一種方式,這些命令用于協調使用關聯的 SQL Server 數據庫對 DataSet 執行的更改。??????????????
????????????????daAuthors.Update(dsPubs, "student"); //數據適配器.Update()方法????????????????
??????????????? //MessageBox.Show("數據庫更新成功!");

????????????????//-------重新綁定dataGridView的數據源,以便重新顯示-------
??????????????? daAuthors.Fill(dsPubs, "student");
??????????????? DataTable tblAuthors1;
??????????????? tblAuthors1 = dsPubs.Tables["student"];
??????????????? dataGridView1.DataSource = tblAuthors1;
??????????? }

??????????? conn.Close();
??????????? conn.Dispose();
??????????? //MessageBox.Show("數據庫連接已關閉");????????????
??????? }

注:

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 的關聯,并且不再使用所生成的命令。

轉載于:https://www.cnblogs.com/Garden-blog/archive/2011/04/10/2011668.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的C#.net删除dataGridView中选中行并同时更新数据库的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。