dataset的使用方法

 System.Data.DataSet
[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 對象互相關聯。您還可通過使用 UniqueConstraintForeignKeyConstraint 對象在 DataSet 中實施數據完整性。有關使用 DataSet 對象的詳細信息,請參見創建和使用 DataSet。

儘管 DataTable 對象中包含數據,但是 DataRelationCollection 允許您遍覽表的層次結構。這些表包含在通過 Tables 屬性訪問的 DataTableCollection 中。當訪問 DataTable 對象時,注意它們是按條件區分大小寫的。例如,如果一個 DataTable 被命名爲“mydatatable”,另一個被命名爲“Mydatatable”,則用於搜索其中一個表的字符串被認爲是區分大小寫的。但是,如果“mydatatable”存在而“Mydatatable”不存在,則認爲該搜索字符串不區分大小寫。有關使用 DataTable 對象的更多信息,請參見創建數據表。

DataSet 可將數據和架構作爲 XML 文檔進行讀寫。數據和架構可通過 HTTP 傳輸,並在支持 XML 的任何平臺上被任何應用程序使用。可使用 WriteXmlSchema 方法將架構保存爲 XML 架構,並且可以使用 WriteXml 方法保存架構和數據。若要讀取既包含架構也包含數據的 XML 文檔,請使用 ReadXml 方法。

在典型的多層實現中,用於創建和刷新 DataSet 並依次更新原始數據的步驟包括:

  1. 通過 DataAdapter 使用數據源中的數據生成和填充 DataSet 中的每個 DataTable
  2. 通過添加、更新或刪除 DataRow 對象更改單個 DataTable 對象中的數據。
  3. 調用 GetChanges 方法以創建只反映對數據進行的更改的第二個 DataSet
  4. 調用 DataAdapterUpdate 方法,並將第二個 DataSet 作爲參數傳遞。
  5. 調用 Merge 方法將第二個 DataSet 中的更改合併到第一個中。
  6. 針對 DataSet 調用 AcceptChanges。或者,調用 RejectChanges 以取消更改。
注意    DataSetDataTable 對象從 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.csdn.net/TrackBack.aspx?PostId=2294997

發佈了2 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章