表示數(shù)據(jù)在內(nèi)存中的緩存。
線程安全
該類(lèi)型對(duì)于多線程讀操作是安全的。您必須使任何寫(xiě)操作同步。
備注
DataSet 是 ADO.NET 結(jié)構(gòu)的主要組件,它是從數(shù)據(jù)源中檢索到的數(shù)據(jù)在內(nèi)存中的緩存。DataSet 由一組 DataTable 對(duì)象組成,您可使這些對(duì)象與 DataRelation 對(duì)象互相關(guān)聯(lián)。您還可通過(guò)使用 UniqueConstraint 和 ForeignKeyConstraint 對(duì)象在 DataSet 中實(shí)施數(shù)據(jù)完整性。有關(guān)使用 DataSet 對(duì)象的詳細(xì)信息,請(qǐng)參見(jiàn)創(chuàng)建和使用 DataSet。
盡管 DataTable 對(duì)象中包含數(shù)據(jù),但是 DataRelationCollection 允許您遍覽表的層次結(jié)構(gòu)。這些表包含在通過(guò) Tables 屬性訪問(wèn)的 DataTableCollection 中。當(dāng)訪問(wèn) DataTable 對(duì)象時(shí),注意它們是按條件區(qū)分大小寫(xiě)的。例如,如果一個(gè) DataTable 被命名為“mydatatable”,另一個(gè)被命名為“Mydatatable”,則用于搜索其中一個(gè)表的字符串被認(rèn)為是區(qū)分大小寫(xiě)的。但是,如果“mydatatable”存在而“Mydatatable”不存在,則認(rèn)為該搜索字符串不區(qū)分大小寫(xiě)。有關(guān)使用 DataTable 對(duì)象的更多信息,請(qǐng)參見(jiàn)創(chuàng)建數(shù)據(jù)表。
DataSet 可將數(shù)據(jù)和架構(gòu)作為 XML 文檔進(jìn)行讀寫(xiě)。數(shù)據(jù)和架構(gòu)可通過(guò) HTTP 傳輸,并在支持 XML 的任何平臺(tái)上被任何應(yīng)用程序使用。可使用 WriteXmlSchema 方法將架構(gòu)保存為 XML 架構(gòu),并且可以使用 WriteXml 方法保存架構(gòu)和數(shù)據(jù)。若要讀取既包含架構(gòu)也包含數(shù)據(jù)的 XML 文檔,請(qǐng)使用 ReadXml 方法。
在典型的多層實(shí)現(xiàn)中,用于創(chuàng)建和刷新 DataSet 并依次更新原始數(shù)據(jù)的步驟包括:
- 通過(guò) DataAdapter 使用數(shù)據(jù)源中的數(shù)據(jù)生成和填充 DataSet 中的每個(gè) DataTable。
- 通過(guò)添加、更新或刪除 DataRow 對(duì)象更改單個(gè) DataTable 對(duì)象中的數(shù)據(jù)。
- 調(diào)用 GetChanges 方法以創(chuàng)建只反映對(duì)數(shù)據(jù)進(jìn)行的更改的第二個(gè) DataSet。
- 調(diào)用 DataAdapter 的 Update 方法,并將第二個(gè) DataSet 作為參數(shù)傳遞。
- 調(diào)用 Merge 方法將第二個(gè) DataSet 中的更改合并到第一個(gè)中。
- 針對(duì) DataSet 調(diào)用 AcceptChanges。或者,調(diào)用 RejectChanges 以取消更改。
注意 DataSet 和 DataTable 對(duì)象從 MarshalByValueComponent 繼承而來(lái),并支持用于遠(yuǎn)程處理的 ISerializable 接口。這些是僅有的可以遠(yuǎn)程處理的 ADO.NET 對(duì)象。
以下示例由幾種方法組成,結(jié)合使用這些方法,從作為 SQLServer 7.0 的示例數(shù)據(jù)庫(kù)而安裝的 Northwind 數(shù)據(jù)庫(kù)創(chuàng)建和填充 DataSet。
[C#]
using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;
public class DataGridSample:Form{
DataSet ds;
DataGrid myGrid;
static void Main(){
Application.Run(new DataGridSample());
}
public DataGridSample(){
InitializeComponent();
}
void InitializeComponent(){
this.ClientSize = new System.Drawing.Size(550, 450);
myGrid = new DataGrid();
myGrid.Location = new Point (10,10);
myGrid.Size = new Size(500, 400);
myGrid.CaptionText = "Microsoft .NET DataGrid";
this.Text = "C# Grid Example";
this.Controls.Add(myGrid);
ConnectToData();
myGrid.SetDataBinding(ds, "Suppliers");
}
void ConnectToData(){
// Create the ConnectionString and create a SqlConnection.
// Change the data source value to the name of your computer.
string cString = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer";
SqlConnection myConnection = new SqlConnection(cString);
// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.TableMappings.Add("Table", "Suppliers");
myConnection.Open();
SqlCommand myCommand = new SqlCommand("SELECT * FROM Suppliers",
myConnection);
myCommand.CommandType = CommandType.Text;
myAdapter.SelectCommand = myCommand;
Console.WriteLine("The connection is open");
ds = new DataSet("Customers");
myAdapter.Fill(ds);
// Create a second Adapter and Command.
SqlDataAdapter adpProducts = new SqlDataAdapter();
adpProducts.TableMappings.Add("Table", "Products");
SqlCommand cmdProducts = new SqlCommand("SELECT * FROM Products",
myConnection);
adpProducts.SelectCommand = cmdProducts;
adpProducts.Fill(ds);
myConnection.Close();
Console.WriteLine("The connection is closed.");
System.Data.DataRelation dr;
System.Data.DataColumn dc1;
System.Data.DataColumn dc2;
// Get the parent and child columns of the two tables.
dc1 = ds.Tables["Suppliers"].Columns["SupplierID"];
dc2 = ds.Tables["Products"].Columns["SupplierID"];
dr = new System.Data.DataRelation("suppliers2products", dc1, dc2);
ds.Relations.Add(dr);
}
}
DataTableCollection
一個(gè) ADO.NET DataSet 包含 DataTable 對(duì)象所表示的零個(gè)或更多個(gè)表的集合。DataTableCollection 包含 DataSet 中的所有 DataTable 對(duì)象。
DataTable 在 System.Data 命名空間中定義,表示內(nèi)存駐留數(shù)據(jù)表。它包含 DataColumnCollection 所表示的列和 ConstraintCollection 所表示的約束的集合,這些列和約束一起定義了該表的架構(gòu)。DataTable 還包含 DataRowCollection 所表示的行的集合,而 DataRowCollection 則包含表中的數(shù)據(jù)。除了其當(dāng)前狀態(tài)之前,DataRow 還會(huì)保留其當(dāng)前版本和初始版本,以標(biāo)識(shí)對(duì)行中存儲(chǔ)的值的更改。