
DataSet(NET数据库应用)
|
DataSet 表示数据在内存中的缓存。 System.Object [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]publicSerializable class 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 并依次更新原始数据的步骤包括:
注意 DataSet 和 DataTable 对象从 MarshalByValueComponent 继承而来,并支持用于远程处理的 ISerializable 接口。这些是仅有的可以远程处理的 ADO.NET 对象。 示例[Visual Basic, C#] 以下示例由几种方法组成,结合使用这些方法,从作为 SQLServer 7.0 的示例数据库而安装的 Northwind 数据库创建和填充 DataSet。 [Visual Basic] Imports SystemImports System.DataImports System.Data.SqlClientImports System.DrawingImports System.ComponentModelImports System.Windows.Forms public 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++, JScript] 没有可用于 C++ 或 JScript 的示例。若要查看 Visual Basic 或 C# 示例,请单击页左上角的“语言筛选器”按钮 要求命名空间: System.Data 平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版 - Windows CE .NET 程序集: System.Data (在 System.Data.dll 中) |
一共有 0 条评论