Datasetの使い方

10985 ワード


System.Data.DataSet
[Visual Basic]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ではテーブルの階層を参照できます.これらのテーブルは、TableプロパティからアクセスするDataTable Collectionに含まれます.DataTableオブジェクトにアクセスするときは、大文字と小文字が条件によって区別されていることに注意してください.たとえば、1つのDataTableが「mydatatable」と命名され、もう1つが「Mydatatable」と命名された場合、1つのテーブルを検索する文字列は大文字と小文字を区別するものとみなされます.ただし、「mydatatable」が存在し、「Mydatatable」が存在しない場合、検索文字列は大文字と小文字を区別しないと考えられます.DataTableオブジェクトの使用方法の詳細については、「データテーブルの作成」を参照してください.
DataSetは、データおよびアーキテクチャをXMLドキュメントとして読み書きすることができる.データおよびアーキテクチャは、HTTPを介して転送され、XMLをサポートする任意のプラットフォーム上で任意のアプリケーションによって使用されます.WriteXmlSchemaメソッドを使用してスキーマをXMLスキーマとして保存し、WriteXmlメソッドを使用してスキーマとデータを保存できます.スキーマとデータを含むXMLドキュメントを読み込むには、ReadXmlメソッドを使用します.
典型的な多層インプリメンテーションでは、DataSetを作成およびリフレッシュし、元のデータを順次更新するには、次の手順に従います.
  • DataAdapterは、データソース内のデータを使用して、DataSet内の各DataTableを生成および埋め込む.
  • DataRowオブジェクトを追加、更新または削除することによって、単一のDataTableオブジェクトのデータを変更します.
  • GetChangesメソッドを呼び出して、データに対する変更のみを反映する2番目のDataSetを作成します.
  • は、DataAdapterのUpdateメソッドを呼び出し、2番目のDataSetをパラメータとして渡す.
  • は、Mergeメソッドを呼び出して、2番目のDataSetの変更を1番目にマージする.
  • は、DataSetに対してAcceptChangesを呼び出す.または、RejectChangesを呼び出して変更をキャンセルします.

  • に注意
    DataSetと
    DataTableオブジェクト
    MarshalByValueComponentが継承し、リモート処理用の
    ISerializableインタフェース.これらは遠隔処理可能なADOのみである.NETオブジェクト.


    [Visual Basic,C#,C++]次の例は、SQLServer 7.0のサンプルデータベースとしてインストールされているNorthwindデータベースから、いくつかの方法で構成されています.
    [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 #using #using #using #using #using 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