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