替別人做網(wǎng)站管理員網(wǎng)站建設(shè)基本技術(shù)
鶴壁市浩天電氣有限公司
2026/01/24 08:30:16
替別人做網(wǎng)站管理員,網(wǎng)站建設(shè)基本技術(shù),網(wǎng)站開(kāi)發(fā)發(fā)展方向,做車貼網(wǎng)站在 C# 的ADO.NET中#xff0c;DataSet是一個(gè)核心的內(nèi)存中數(shù)據(jù)緩存對(duì)象#xff0c;用于離線存儲(chǔ)和處理數(shù)據(jù)#xff0c;常被稱為 “內(nèi)存中的數(shù)據(jù)庫(kù)”。它可以包含多個(gè)數(shù)據(jù)表、表之間的關(guān)系、約束等#xff0c;獨(dú)立于數(shù)據(jù)源#xff08;如 SQL Server、Oracle 等#xff09;DataSet是一個(gè)核心的內(nèi)存中數(shù)據(jù)緩存對(duì)象用于離線存儲(chǔ)和處理數(shù)據(jù)常被稱為 “內(nèi)存中的數(shù)據(jù)庫(kù)”。它可以包含多個(gè)數(shù)據(jù)表、表之間的關(guān)系、約束等獨(dú)立于數(shù)據(jù)源如 SQL Server、Oracle 等非常適合需要在客戶端維護(hù)復(fù)雜數(shù)據(jù)關(guān)系或離線操作的場(chǎng)景。一、DataSet 核心概念DataSet本質(zhì)是一個(gè)離線數(shù)據(jù)容器主要特點(diǎn)包含一個(gè)或多個(gè)DataTable數(shù)據(jù)表表結(jié)構(gòu)與數(shù)據(jù)庫(kù)表類似支持DataRelation表關(guān)系可定義表之間的關(guān)聯(lián)如主外鍵關(guān)系自帶數(shù)據(jù)架構(gòu)信息Schema記錄字段類型、約束等元數(shù)據(jù)獨(dú)立于數(shù)據(jù)源斷開(kāi)連接后仍可操作數(shù)據(jù)適合離線場(chǎng)景提供數(shù)據(jù)修改跟蹤通過(guò)RowState便于批量更新回?cái)?shù)據(jù)源二、DataSet 相關(guān)核心類DataSet的功能依賴于一系列相關(guān)類協(xié)同工作形成完整的內(nèi)存數(shù)據(jù)模型類名作用描述DataSet頂層容器包含多個(gè)DataTable、DataRelation和約束管理整體數(shù)據(jù)狀態(tài)DataTable數(shù)據(jù)表對(duì)應(yīng)數(shù)據(jù)庫(kù)中的表包含DataColumn列和DataRow行DataColumn表中的列定義數(shù)據(jù)類型、默認(rèn)值、約束主鍵、唯一鍵等DataRow表中的行存儲(chǔ)具體數(shù)據(jù)提供數(shù)據(jù)修改、狀態(tài)跟蹤新增 / 修改 / 刪除功能DataRelation定義兩個(gè)DataTable之間的關(guān)系如父子表支持關(guān)聯(lián)數(shù)據(jù)導(dǎo)航DataViewDataTable的視圖用于排序、篩選、搜索數(shù)據(jù)可綁定到 UI 控件DataAdapter數(shù)據(jù)適配器負(fù)責(zé)DataSet與數(shù)據(jù)源之間的同步填充數(shù)據(jù)、更新修改三、核心類詳解與使用示例1. DataSet 與 DataTableDataSet是容器DataTable是其中的表一個(gè)DataSet可包含多個(gè)DataTable。示例創(chuàng)建 DataSet 和 DataTable// 1. 創(chuàng)建DataSet DataSet dataSet new DataSet(SalesDB); // 指定數(shù)據(jù)集名稱 ? // 2. 創(chuàng)建DataTable客戶表 DataTable customerTable new DataTable(Customers); ? // 3. 定義DataColumn列 DataColumn idCol new DataColumn(Id, typeof(int)); idCol.AllowDBNull false; // 不允許為空 idCol.Unique true; // 唯一約束 customerTable.Columns.Add(idCol); ? // 設(shè)置主鍵可多個(gè)列組成復(fù)合主鍵 customerTable.PrimaryKey new[] { idCol }; ? // 添加其他列 customerTable.Columns.Add(Name, typeof(string)); customerTable.Columns.Add(RegisterDate, typeof(DateTime)); ? // 4. 向DataTable添加DataRow行數(shù)據(jù) DataRow row1 customerTable.NewRow(); row1[Id] 1; row1[Name] 張三; row1[RegisterDate] new DateTime(2023, 1, 15); customerTable.Rows.Add(row1); ? DataRow row2 customerTable.NewRow(); row2[Id] 2; row2[Name] 李四; row2[RegisterDate] new DateTime(2023, 3, 20); customerTable.Rows.Add(row2); ? // 5. 將DataTable添加到DataSet dataSet.Tables.Add(customerTable); ? // 驗(yàn)證輸出表信息 Console.WriteLine($數(shù)據(jù)集名稱{dataSet.DataSetName}); Console.WriteLine($包含表數(shù)量{dataSet.Tables.Count}); Console.WriteLine($客戶表行數(shù){dataSet.Tables[Customers].Rows.Count});2. DataRow操作行數(shù)據(jù)DataRow存儲(chǔ)具體數(shù)據(jù)提供數(shù)據(jù)訪問(wèn)、修改和狀態(tài)跟蹤功能。示例操作 DataRow// 獲取前面創(chuàng)建的客戶表 DataTable customers dataSet.Tables[Customers]; ? // 1. 訪問(wèn)行數(shù)據(jù) DataRow firstRow customers.Rows[0]; Console.WriteLine($第一個(gè)客戶{firstRow[Name]}ID{firstRow[Id]}); ? // 2. 修改數(shù)據(jù) firstRow[Name] 張三三; // 修改名稱 Console.WriteLine($修改后名稱{firstRow[Name]}); ? // 3. 新增行簡(jiǎn)化寫法 customers.Rows.Add(3, 王五, new DateTime(2023, 5, 10)); ? // 4. 刪除行標(biāo)記刪除需調(diào)用AcceptChanges才會(huì)真正刪除 DataRow rowToDelete customers.Rows.Find(2); // 通過(guò)主鍵查找 if (rowToDelete ! null) rowToDelete.Delete(); ? // 5. 查看行狀態(tài)新增/修改/刪除/未變 foreach (DataRow row in customers.Rows) { Console.WriteLine($ID: {row[Id]}, 狀態(tài): {row.RowState}); } // 輸出 // ID: 1, 狀態(tài): Modified已修改 // ID: 2, 狀態(tài): Deleted已刪除 // ID: 3, 狀態(tài): Added新增 ? // 6. 提交/撤銷修改 customers.AcceptChanges(); // 提交所有修改刪除標(biāo)記行將被移除 // customers.RejectChanges(); // 撤銷所有修改恢復(fù)到上次Accept狀態(tài)3. DataRelation表關(guān)系DataRelation用于定義兩個(gè)DataTable之間的關(guān)聯(lián)如訂單表與客戶表的 “一對(duì)多” 關(guān)系支持通過(guò)父行查找子行。示例創(chuàng)建表關(guān)系// 1. 創(chuàng)建訂單表 DataTable orderTable new DataTable(Orders); orderTable.Columns.Add(OrderId, typeof(int)); orderTable.Columns.Add(CustomerId, typeof(int)); // 外鍵關(guān)聯(lián)Customers表的Id orderTable.Columns.Add(Amount, typeof(decimal)); orderTable.PrimaryKey new[] { orderTable.Columns[OrderId] }; ? // 添加訂單數(shù)據(jù) orderTable.Rows.Add(1001, 1, 999.99m); // 客戶1的訂單 orderTable.Rows.Add(1002, 1, 1599.50m); // 客戶1的另一個(gè)訂單 orderTable.Rows.Add(1003, 3, 599.00m); // 客戶3的訂單 dataSet.Tables.Add(orderTable); ? // 2. 創(chuàng)建關(guān)系客戶表 - 訂單表一對(duì)多 DataRelation customerOrderRel new DataRelation( Customer_Order, // 關(guān)系名稱 dataSet.Tables[Customers].Columns[Id], // 父表主鍵 dataSet.Tables[Orders].Columns[CustomerId] // 子表外鍵 ); dataSet.Relations.Add(customerOrderRel); ? // 3. 通過(guò)關(guān)系導(dǎo)航數(shù)據(jù)查找客戶1的所有訂單 DataRow customer dataSet.Tables[Customers].Rows.Find(1); if (customer ! null) { // 獲取子行訂單 DataRow[] orders customer.GetChildRows(customerOrderRel); Console.WriteLine($客戶 {customer[Name]} 的訂單數(shù)量{orders.Length}); foreach (DataRow order in orders) { Console.WriteLine($訂單ID{order[OrderId]}金額{order[Amount]}); } } ? // 4. 通過(guò)子行查找父行查找訂單1001的客戶 DataRow order dataSet.Tables[Orders].Rows.Find(1001); if (order ! null) { DataRow parentCustomer order.GetParentRow(customerOrderRel); Console.WriteLine($訂單1001的客戶{parentCustomer[Name]}); }4. DataView數(shù)據(jù)視圖DataView是DataTable的可定制視圖支持排序、篩選、搜索常用于 UI 綁定如DataGridView。示例使用 DataView 篩選和排序// 獲取訂單表 DataTable orders dataSet.Tables[Orders]; ? // 1. 創(chuàng)建DataView默認(rèn)包含所有行 DataView orderView new DataView(orders); ? // 2. 篩選只顯示金額1000的訂單 orderView.RowFilter Amount 1000; ? // 3. 排序按金額降序 orderView.Sort Amount DESC; ? // 4. 遍歷視圖數(shù)據(jù) Console.WriteLine(篩選并排序后的訂單); foreach (DataRowView rowView in orderView) { Console.WriteLine($訂單ID{rowView[OrderId]}金額{rowView[Amount]}); } ? // 5. 綁定到UI控件如WinForm的DataGridView // dataGridView1.DataSource orderView;5. DataAdapter同步數(shù)據(jù)源DataAdapter是DataSet與數(shù)據(jù)源之間的橋梁負(fù)責(zé)將數(shù)據(jù)源數(shù)據(jù)填充到DataSet并將DataSet的修改更新回?cái)?shù)據(jù)源。示例用 DataAdapter 填充和更新數(shù)據(jù)string connectionString Server.;DatabaseSalesDB;Integrated SecurityTrue;; ? // 1. 創(chuàng)建DataAdapter關(guān)聯(lián)查詢命令 using (SqlDataAdapter adapter new SqlDataAdapter()) { // 查詢命令用于填充數(shù)據(jù) adapter.SelectCommand new SqlCommand( SELECT Id, Name, RegisterDate FROM Customers, new SqlConnection(connectionString) ); ? // 更新命令用于將修改同步回?cái)?shù)據(jù)庫(kù)可自動(dòng)生成或手動(dòng)指定 SqlCommandBuilder cmdBuilder new SqlCommandBuilder(adapter); // 自動(dòng)生成增刪改命令 ? // 2. 填充DataSet DataSet dbDataSet new DataSet(); adapter.Fill(dbDataSet, Customers); // 填充到DataSet的Customers表 ? // 3. 修改數(shù)據(jù)在內(nèi)存中 DataTable dbCustomers dbDataSet.Tables[Customers]; dbCustomers.Rows[0][Name] 張三更新; // 修改 dbCustomers.Rows.Add(4, 趙六, DateTime.Now); // 新增 dbCustomers.Rows[1].Delete(); // 刪除 ? // 4. 將修改更新回?cái)?shù)據(jù)庫(kù)根據(jù)RowState批量執(zhí)行增刪改 int updatedRows adapter.Update(dbDataSet, Customers); Console.WriteLine($成功更新 {updatedRows} 條記錄到數(shù)據(jù)庫(kù)); }四、DataSet 與相關(guān)類的關(guān)系總結(jié)層級(jí)關(guān)系DataSet包含DataTableDataTable包含DataColumn和DataRowDataRelation關(guān)聯(lián)多個(gè)DataTable。數(shù)據(jù)流轉(zhuǎn)DataAdapter從數(shù)據(jù)源讀取數(shù)據(jù)填充DataSet修改后再通過(guò)DataAdapter更新回?cái)?shù)據(jù)源。UI 交互DataView作為DataTable的視圖簡(jiǎn)化 UI 綁定和數(shù)據(jù)展示。五、使用場(chǎng)景與注意事項(xiàng)適用場(chǎng)景離線數(shù)據(jù)處理、復(fù)雜數(shù)據(jù)關(guān)系維護(hù)多表關(guān)聯(lián)、需要緩存數(shù)據(jù)的客戶端應(yīng)用。局限性內(nèi)存占用較大不適合處理超大量數(shù)據(jù)此時(shí)推薦DataReader離線操作可能導(dǎo)致數(shù)據(jù)一致性問(wèn)題需處理并發(fā)沖突。性能建議只加載需要的數(shù)據(jù)避免頻繁創(chuàng)建DataSet批量更新時(shí)使用DataAdapter.Update而非逐條操作。通過(guò)DataSet及其相關(guān)類開(kāi)發(fā)者可以在內(nèi)存中構(gòu)建完整的關(guān)系型數(shù)據(jù)模型靈活實(shí)現(xiàn)離線數(shù)據(jù)處理和復(fù)雜業(yè)務(wù)邏輯。