表示數(shù)據(jù)在內(nèi)存中的緩存。
線程安全
該類型對于多線程讀操作是安全的。您必須使任何寫操作同步。
備注
DataSet 是 ADO.NET 結(jié)構(gòu)的主要組件,它是從數(shù)據(jù)源中檢索到的數(shù)據(jù)在內(nèi)存中的緩存。DataSet 由一組 DataTable 對象組成,您可使這些對象與 DataRelation 對象互相關(guān)聯(lián)。您還可通過使用 UniqueConstraint 和 ForeignKeyConstraint 對象在 DataSet 中實施數(shù)據(jù)完整性。有關(guān)使用 DataSet 對象的詳細信息,請參見創(chuàng)建和使用 DataSet。
盡管 DataTable 對象中包含數(shù)據(jù),但是 DataRelationCollection 允許您遍覽表的層次結(jié)構(gòu)。這些表包含在通過 Tables 屬性訪問的 DataTableCollection 中。當訪問 DataTable 對象時,注意它們是按條件區(qū)分大小寫的。例如,如果一個 DataTable 被命名為“mydatatable”,另一個被命名為“Mydatatable”,則用于搜索其中一個表的字符串被認為是區(qū)分大小寫的。但是,如果“mydatatable”存在而“Mydatatable”不存在,則認為該搜索字符串不區(qū)分大小寫。有關(guān)使用 DataTable 對象的更多信息,請參見創(chuàng)建數(shù)據(jù)表。
DataSet 可將數(shù)據(jù)和架構(gòu)作為 XML 文檔進行讀寫。數(shù)據(jù)和架構(gòu)可通過 HTTP 傳輸,并在支持 XML 的任何平臺上被任何應用程序使用??墒褂?WriteXmlSchema 方法將架構(gòu)保存為 XML 架構(gòu),并且可以使用 WriteXml 方法保存架構(gòu)和數(shù)據(jù)。若要讀取既包含架構(gòu)也包含數(shù)據(jù)的 XML 文檔,請使用 ReadXml 方法。
在典型的多層實現(xiàn)中,用于創(chuàng)建和刷新 DataSet 并依次更新原始數(shù)據(jù)的步驟包括:
- 通過 DataAdapter 使用數(shù)據(jù)源中的數(shù)據(jù)生成和填充 DataSet 中的每個 DataTable。
- 通過添加、更新或刪除 DataRow 對象更改單個 DataTable 對象中的數(shù)據(jù)。
- 調(diào)用 GetChanges 方法以創(chuàng)建只反映對數(shù)據(jù)進行的更改的第二個 DataSet。
- 調(diào)用 DataAdapter 的 Update 方法,并將第二個 DataSet 作為參數(shù)傳遞。
- 調(diào)用 Merge 方法將第二個 DataSet 中的更改合并到第一個中。
- 針對 DataSet 調(diào)用 AcceptChanges?;蛘?,調(diào)用 RejectChanges 以取消更改。
注意 DataSet 和 DataTable 對象從 MarshalByValueComponent 繼承而來,并支持用于遠程處理的 ISerializable 接口。這些是僅有的可以遠程處理的 ADO.NET 對象。
以下示例由幾種方法組成,結(jié)合使用這些方法,從作為 SQLServer 7.0 的示例數(shù)據(jù)庫而安裝的 Northwind 數(shù)據(jù)庫創(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
一個 ADO.NET DataSet 包含 DataTable 對象所表示的零個或更多個表的集合。DataTableCollection 包含 DataSet 中的所有 DataTable 對象。
DataTable 在 System.Data 命名空間中定義,表示內(nèi)存駐留數(shù)據(jù)表。它包含 DataColumnCollection 所表示的列和 ConstraintCollection 所表示的約束的集合,這些列和約束一起定義了該表的架構(gòu)。DataTable 還包含 DataRowCollection 所表示的行的集合,而 DataRowCollection 則包含表中的數(shù)據(jù)。除了其當前狀態(tài)之前,DataRow 還會保留其當前版本和初始版本,以標識對行中存儲的值的更改。