DataSet 类与DataAdapter的TableMappings属性

表示数据在内存中的缓存。

命名空间:System.Data
程序集:System.Data(在 system.data.dll 中)

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 以取消更改。

using System;
using System.Data;
using System.Data.SqlClient;

namespace Microsoft.AdoNet.DataSetDemo
{
class NorthwindDataSet
{
static void Main()
{
string connectionString = GetConnectionString();
ConnectToData(connectionString);
}

private static void ConnectToData(string connectionString)
{
//Create a SqlConnection to the Northwind database.
using (SqlConnection connection =
new SqlConnection(connectionString))
{
//Create a SqlDataAdapter for the Suppliers table.
SqlDataAdapter adapter = new SqlDataAdapter();

// A table mapping names the DataTable.
adapter.TableMappings.Add("Table", "Suppliers");

// Open the connection.
connection.Open();
Console.WriteLine("The SqlConnection is open.");

// Create a SqlCommand to retrieve Suppliers data.
SqlCommand command = new SqlCommand(
"SELECT SupplierID, CompanyName FROM dbo.Suppliers;",
connection);
command.CommandType = CommandType.Text;

// Set the SqlDataAdapter's SelectCommand.
adapter.SelectCommand = command;

// Fill the DataSet.
DataSet dataSet = new DataSet("Suppliers");
adapter.Fill(dataSet);

// Create a second Adapter and Command to get
// the Products table, a child table of Suppliers.
SqlDataAdapter productsAdapter = new SqlDataAdapter();
productsAdapter.TableMappings.Add("Table", "Products");

SqlCommand productsCommand = new SqlCommand(
"SELECT ProductID, SupplierID FROM dbo.Products;",
connection);
productsAdapter.SelectCommand = productsCommand;

// Fill the DataSet.
productsAdapter.Fill(dataSet);

// Close the connection.
connection.Close();
Console.WriteLine("The SqlConnection is closed.");

// Create a DataRelation to link the two tables
// based on the SupplierID.
DataColumn parentColumn =
dataSet.Tables["Suppliers"].Columns["SupplierID"];
DataColumn childColumn =
dataSet.Tables["Products"].Columns["SupplierID"];
DataRelation relation =
new System.Data.DataRelation("SuppliersProducts",
parentColumn, childColumn);
dataSet.Relations.Add(relation);
Console.WriteLine(
"The {0} DataRelation has been created.",
relation.RelationName);
}
}

static private string GetConnectionString()
{
// To avoid storing the connection string in your code,
// you can retrieve it from a configuration file.
return "Data Source=(local);Initial Catalog=Northwind;"
+ "Integrated Security=SSPI";
}
}
}
DataAdapter的TableMappings属性:
如果一个DataAdapter返回的是多个记录集,那么,可以通过这个TableMappings来和这多个记录集建立名称对应关系.

默认的情况下,比如没有指定这个值,那么Fill(DataSet ds)后ds中的第一个表名是Table,第二个表是Table1,第三个是Table2...
如果用了TableMappings,那么这些Table,Table1,Table2就可以用TableMappings中指定的名称了,
这样的话,就可以通过ds.Table[tableName]来访问一个具体名称的表了.

例如:
sqlAda = new SqlDataAdapter();
sqlAda.TableMappings.Add("Table", "orders");
sqlAda.TableMappings.Add("Table1", "employees");
...
ds = new DataSet();
sqlAda.Fill(ds);
//这时ds.Tables[0].TableName为orders,
//ds.Tables[1].TableName为employees;
//这样就方便了按句子取一个表,比如DataTable orders=ds.Tables["orders"];


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值