[Visual Basic]<Serializable>Public Class DataSet Inherits MarshalByValueComponent Implements IListSource, ISupportInitialize, ISerializable[C#][Serializable]public class DataSet : MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable[C++][Serializable]public __gc class DataSet : public MarshalByValueComponent, IListSource, ISupportInitialize, ISerializable[JScript]public Serializableclass DataSet extends MarshalByValueComponent implements IListSource, ISupportInitialize, ISerializable
线程安全
该类型对于多线程读操作是安全的。您必须使任何写操作同步。
备注
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 对象。
示例
[Visual Basic, C#, C++] 以下示例由几种方法组成,结合使用这些方法,从作为 SQLServer 7.0 的示例数据库而安装的 Northwind 数据库创建和填充 DataSet。
[Visual Basic] Imports SystemImports System.DataImports System.Data.SqlClientImports System.DrawingImports System.ComponentModelImports System.Windows.Formspublic class DataGridSample Inherits Form Private ds As DataSet Private myGrid As DataGrid Shared Sub Main Application.Run(new DataGridSample()) End Sub public Sub New() InitializeComponent() End Sub public Sub InitializeComponent() Me.ClientSize = new Size(550, 450) myGrid = new DataGrid() myGrid.Location = new Point (10,10) myGrid.Size = new Size(500, 400) myGrid.CaptionText = "Microsoft .NET DataGrid" Me.Controls.Add(myGrid) Me.Text = "Visual Basic Grid Example" ConnectToData() myGrid.SetDataBinding(ds, "Suppliers") End Sub Private Sub ConnectToData() ' Create the ConnectionString and create a SqlConnection. ' Change the data source value to the name of your computer. Dim cString As string = "Persist Security Info=False;Integrated Security=SSPI;database=northwind;server=mySQLServer" Dim cnNorthwind As SqlConnection = new SqlConnection(cString) ' Create a SqlDataAdapter for the Suppliers table. Dim adpSuppliers As SqlDataAdapter = new SqlDataAdapter() ' A table mapping tells the adapter what to call the table. adpSuppliers.TableMappings.Add("Table", "Suppliers") cnNorthwind.Open() Dim cmdSuppliers As SqlCommand = _ new SqlCommand("SELECT * FROM Suppliers", cnNorthwind) cmdSuppliers.CommandType = CommandType.Text adpSuppliers.SelectCommand = cmdSuppliers Console.WriteLine("The connection is open.") ds = New DataSet("Customers") adpSuppliers.Fill(ds) ' Create a second SqlDataAdapter and SqlCommand to get ' the Products table, a child table of Suppliers. Dim adpProducts As SqlDataAdapter = new SqlDataAdapter() adpProducts.TableMappings.Add("Table", "Products") Dim cmdProducts As SqlCommand = _ new SqlCommand("SELECT * FROM Products", cnNorthwind) adpProducts.SelectCommand = cmdProducts adpProducts.Fill(ds) cnNorthwind.Close() Console.WriteLine("The connection is closed.") ' You must create a DataRelation to link the two tables. Dim dr As DataRelation Dim dc1 As DataColumn Dim dc2 As DataColumn ' 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) End SubEnd Class[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); }}[C++] #using <mscorlib.dll>#using <System.Xml.dll>#using <System.dll>#using <System.Windows.Forms.dll>#using <System.Drawing.dll>#using <System.Data.dll>using namespace System;using namespace System::Data;using namespace System::Data::SqlClient;using namespace System::Drawing;using namespace System::Windows::Forms;public __gc class DataGridSample:public Form{ DataSet* ds; DataGrid* myGrid;public: DataGridSample(){ InitializeComponent(); } void InitializeComponent(){ this->ClientSize = System::Drawing::Size(550, 450); myGrid = new DataGrid(); myGrid->Location = Point (10,10); myGrid->Size = System::Drawing::Size(500, 400); myGrid->CaptionText = S"Microsoft .NET DataGrid"; this->Text = S"C# Grid Example"; this->Controls->Add(myGrid); ConnectToData(); myGrid->SetDataBinding(ds, S"Suppliers"); } void ConnectToData(){ // Create the ConnectionString and create a SqlConnection. // Change the data source value to the name of your computer. String* cString = S"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(S"Table", S"Suppliers"); myConnection->Open(); SqlCommand* myCommand = new SqlCommand(S"SELECT * FROM Suppliers", myConnection); myCommand->CommandType = CommandType::Text; myAdapter->SelectCommand = myCommand; Console::WriteLine(S"The connection is open"); ds = new DataSet(S"Customers"); myAdapter->Fill(ds); // Create a second Adapter and Command. SqlDataAdapter* adpProducts = new SqlDataAdapter(); adpProducts->TableMappings->Add(S"Table", S"Products"); SqlCommand* cmdProducts = new SqlCommand(S"SELECT * FROM Products", myConnection); adpProducts->SelectCommand = cmdProducts; adpProducts->Fill(ds); myConnection->Close(); Console::WriteLine(S"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->Item[S"Suppliers"]->Columns->Item[S"SupplierID"]; dc2 = ds->Tables->Item[S"Products"]->Columns->Item[S"SupplierID"]; dr = new System::Data::DataRelation(S"suppliers2products", dc1, dc2); ds->Relations->Add(dr); }};int main(){ Application::Run(new DataGridSample());}
Trackback: http://tb.blog.youkuaiyun.com/TrackBack.aspx?PostId=2294997