ITPub博客

首页 > Linux操作系统 > Linux操作系统 > DataSet

DataSet

原创 Linux操作系统 作者:iDotNetSpace 时间:2010-05-07 14:01:53 0 删除 编辑
using System;
02 using System.Data;
03 using System.Data.SqlClient;
04   
05 namespace Microsoft.AdoNet.DataSetDemo
06 {
07     class NorthwindDataSet
08     {
09         static void Main()
10         {
11             string connectionString = GetConnectionString();
12             ConnectToData(connectionString);
13         }
14   
15         private static void ConnectToData(string connectionString)
16         {
17             //Create a SqlConnection to the Northwind database.
18             using (SqlConnection connection =
19                        new SqlConnection(connectionString))
20             {
21                 //Create a SqlDataAdapter for the Suppliers table.
22                 SqlDataAdapter adapter = new SqlDataAdapter();
23   
24                 // A table mapping names the DataTable.
25                 adapter.TableMappings.Add("Table", "Suppliers");
26   
27                 // Open the connection.
28                 connection.Open();
29                 Console.WriteLine("The SqlConnection is open.");
30   
31                 // Create a SqlCommand to retrieve Suppliers data.
32                 SqlCommand command = new SqlCommand(
33                     "SELECT SupplierID, CompanyName FROM dbo.Suppliers;",
34                     connection);
35                 command.CommandType = CommandType.Text;
36   
37                 // Set the SqlDataAdapter's SelectCommand.
38                 adapter.SelectCommand = command;
39   
40                 // Fill the DataSet.
41                 DataSet dataSet = new DataSet("Suppliers");
42                 adapter.Fill(dataSet);
43   
44                 // Create a second Adapter and Command to get
45                 // the Products table, a child table of Suppliers. 
46                 SqlDataAdapter productsAdapter = new SqlDataAdapter();
47                 productsAdapter.TableMappings.Add("Table", "Products");
48   
49                 SqlCommand productsCommand = new SqlCommand(
50                     "SELECT ProductID, SupplierID FROM dbo.Products;",
51                     connection);
52                 productsAdapter.SelectCommand = productsCommand;
53   
54                 // Fill the DataSet.
55                 productsAdapter.Fill(dataSet);
56   
57                 // Close the connection.
58                 connection.Close();
59                 Console.WriteLine("The SqlConnection is closed.");
60   
61                 // Create a DataRelation to link the two tables
62                 // based on the SupplierID.
63                 DataColumn parentColumn =
64                     dataSet.Tables["Suppliers"].Columns["SupplierID"];
65                 DataColumn childColumn =
66                     dataSet.Tables["Products"].Columns["SupplierID"];
67                 DataRelation relation =
68                     new System.Data.DataRelation("SuppliersProducts",
69                     parentColumn, childColumn);
70                 dataSet.Relations.Add(relation);
71                 Console.WriteLine(
72                     "The {0} DataRelation has been created.",
73                     relation.RelationName);
74             }
75         }
76   
77         static private string GetConnectionString()
78         {
79             // To avoid storing the connection string in your code, 
80             // you can retrieve it from a configuration file.
81             return "Data Source=(local);Initial Catalog=Northwind;"
82                 + "Integrated Security=SSPI";
83         }
84     }
85 }

 

DataSet 是 ADO.NET 结构的主要组件,它是从数据源中检索到的数据在内存中的缓存。DataSet 由一组 DataTable 对象组成,您可使这些对象与 DataRelation 对象互相关联。您还可通过使用 UniqueConstraint 和 ForeignKeyConstraint 对象在 DataSet 中实施数据完整性。有关使用 DataSet 对象的详细信息,请参见 在 ADO.NET 中使用 DataSet。

尽管 DataTable 对象中包含数据,但是 DataRelationCollection 允许您遍览表的层次结构。这些表包含在通过 Tables 属性访问的 DataTableCollection 中。当访问 DataTable 对象时,请注意它们是按条件区分大小写的。例如,如果一个 DataTable 被命名为“mydatatable”,另一个被命名为“Mydatatable”,则用于搜索其中一个表的字符串被认为是区分大小写的。但是,如果“mydatatable”存在而“Mydatatable”不存在,则认为该搜索字符串不区分大小写。有关使用 DataTable 对象的更多信息,请参见 创建 DataTable。

DataSet 可将数据和架构作为 XML 文档进行读写。数据和架构可通过 HTTP 传输,并在支持 XML 的任何平台上被任何应用程序使用。可使用 WriteXmlSchema 方法将架构保存为 XML 架构,并且可以使用 WriteXml 方法保存架构和数据。若要读取既包含架构也包含数据的 XML 文档,请使用 ReadXml 方法。

在典型的多层实现中,用于创建和刷新 DataSet 并依次更新原始数据的步骤包括:

1.通过 DataAdapter 使用数据源中的数据生成和填充 DataSet 中的每个 DataTable。

2.通过添加、更新或删除 DataRow 对象更改单个 DataTable 对象中的数据。

3.调用 GetChanges 方法以创建只反映对数据进行的更改的第二个 DataSet。

4.调用 DataAdapter 的 Update 方法,并将第二个 DataSet 作为参数传递。

5.调用 Merge 方法将第二个 DataSet 中的更改合并到第一个中。

6.针对 DataSet 调用 AcceptChanges。或者,调用 RejectChanges 以取消更改。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-662138/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: C#模拟登录总结
请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766061