AddWithKey 相关技术
2009-08-13 15:57
211 查看
DataAdapter 的 Fill 方法仅使用数据源中的表列和表行来填充 DataSet;虽然约束通常由数据源来设置,但在默认情况下,Fill 方法不会将此架构信息添加到 DataSet 中。若要使用数据源中的现有主键约束信息填充 DataSet,则可以调用 DataAdapter 的 FillSchema 方法,或者在调用 Fill 之前将 DataAdapter 的 MissingSchemaAction 属性设置为 AddWithKey。这将确保 DataSet 中的主键约束反映数据源中的主键约束。外键约束信息不包含在内,将需要显式创建,如将约束添加到表所示。
如果在使用数据填充 DataSet 之前向其中添加架构信息,将确保将主键约束与 DataSet 的 DataTable 对象包含在一起。这样,当再次调用 Fill 来填充 DataSet 时,将使用主键列信息来匹配数据源中的新行和每个 DataTable 中的当前行,并且使用数据源中的数据改写表中的当前数据。如果没有架构信息,来自数据源的新行将追加到 DataSet,从而导致重复的行。
注意 如果数据源中的某列被标识为自动递增列,则 MissingSchemaAction 为 AddWithKey 的 FillSchema 方法或 Fill 方法将创建一个 AutoIncrement 属性设置为 true 的 DataColumn。不过,您将需要手动设置 AutoIncrementStep 和 AutoIncrementSeed 值。有关自动递增列的更多信息,请参阅创建 AutoIncrement 列。
当使用 FillSchema 或将 MissingSchemaAction 设置为 AddWithKey 时,将需要在数据源中进行额外的处理来确定主键列信息。这一额外的处理可能会降低性能。如果主键信息在设计时已知,为了实现最佳性能,建议显式指定一个或多个主键列。有关为表显式设置主键信息的信息,请参阅为表定义主键。
以下代码示例显示如何使用 FillSchema 向 DataSet 添加架构信息。
[Visual Basic]
Dim custDS As DataSet = New DataSet()
custDA.FillSchema(custDS, SchemaType.Source, "Customers")
custDA.Fill(custDS, "Customers")
[C#]
DataSet custDS = new DataSet();
custDA.FillSchema(custDS, SchemaType.Source, "Customers");
custDA.Fill(custDS, "Customers");
以下代码示例显示如何使用 Fill 方法的 MissingSchemaAction.AddWithKey 属性向 DataSet 添加架构信息。
[Visual Basic]
Dim custDS As DataSet = New DataSet()
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
custDA.Fill(custDS, "Customers")
[C#]
DataSet custDS = new DataSet();
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
custDA.Fill(custDS, "Customers");
多个结果集
如果 DataAdapter 遇到从 SelectCommand 中返回的多个结果集,它将在 DataSet 中创建多个表。将向这些表提供递增的默认名称 TableN,以表示 Table0 的“Table”为第一个表名。如果以参数形式向 FillSchema 方法传递表名称,则将向这些表提供递增的默认名称 TableNameN,这些表名称以表示 TableName0 的“TableName”为起始。
注意 如果对返回多个结果集的命令调用 OleDbDataAdapter 对象的 FillSchema 方法,则将只返回第一个结果集中的架构信息。当使用 OleDbDataAdapter 为多个结果集返回架构信息时,建议在调用 Fill 方法时将 MissingSchemaAction 指定为 AddWithKey 并获取架构信息。
如果在使用数据填充 DataSet 之前向其中添加架构信息,将确保将主键约束与 DataSet 的 DataTable 对象包含在一起。这样,当再次调用 Fill 来填充 DataSet 时,将使用主键列信息来匹配数据源中的新行和每个 DataTable 中的当前行,并且使用数据源中的数据改写表中的当前数据。如果没有架构信息,来自数据源的新行将追加到 DataSet,从而导致重复的行。
注意 如果数据源中的某列被标识为自动递增列,则 MissingSchemaAction 为 AddWithKey 的 FillSchema 方法或 Fill 方法将创建一个 AutoIncrement 属性设置为 true 的 DataColumn。不过,您将需要手动设置 AutoIncrementStep 和 AutoIncrementSeed 值。有关自动递增列的更多信息,请参阅创建 AutoIncrement 列。
当使用 FillSchema 或将 MissingSchemaAction 设置为 AddWithKey 时,将需要在数据源中进行额外的处理来确定主键列信息。这一额外的处理可能会降低性能。如果主键信息在设计时已知,为了实现最佳性能,建议显式指定一个或多个主键列。有关为表显式设置主键信息的信息,请参阅为表定义主键。
以下代码示例显示如何使用 FillSchema 向 DataSet 添加架构信息。
[Visual Basic]
Dim custDS As DataSet = New DataSet()
custDA.FillSchema(custDS, SchemaType.Source, "Customers")
custDA.Fill(custDS, "Customers")
[C#]
DataSet custDS = new DataSet();
custDA.FillSchema(custDS, SchemaType.Source, "Customers");
custDA.Fill(custDS, "Customers");
以下代码示例显示如何使用 Fill 方法的 MissingSchemaAction.AddWithKey 属性向 DataSet 添加架构信息。
[Visual Basic]
Dim custDS As DataSet = New DataSet()
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey
custDA.Fill(custDS, "Customers")
[C#]
DataSet custDS = new DataSet();
custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
custDA.Fill(custDS, "Customers");
多个结果集
如果 DataAdapter 遇到从 SelectCommand 中返回的多个结果集,它将在 DataSet 中创建多个表。将向这些表提供递增的默认名称 TableN,以表示 Table0 的“Table”为第一个表名。如果以参数形式向 FillSchema 方法传递表名称,则将向这些表提供递增的默认名称 TableNameN,这些表名称以表示 TableName0 的“TableName”为起始。
注意 如果对返回多个结果集的命令调用 OleDbDataAdapter 对象的 FillSchema 方法,则将只返回第一个结果集中的架构信息。当使用 OleDbDataAdapter 为多个结果集返回架构信息时,建议在调用 Fill 方法时将 MissingSchemaAction 指定为 AddWithKey 并获取架构信息。
相关文章推荐
- AddWithKey 相关技术
- AddWithKey 相关技术
- AddWithKey 相关技术
- AddWithKey 相关技术
- AddWithKey 相关技术
- System.ArgumentException: 已添加了具有相同键的项。(An item with the same key has already been added) 在 System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) 在 System.Web.Mvc.Js
- how to solve Error cannot add duplicate collection entry of type add with unique key attribute 'valu
- 让人迷惑的"Cannot add an entity with a key that is already in use."
- django models 操作数据库遇到相关问题 Cannot add foreign key constrain
- 四:动画技术二{动画相关技术UIKit 力学(UIKit Dynamics)和运动效果(Motion Effects)}
- 淘宝开源Key/Value结构数据存储系统Tair技术剖析
- 一起谈.NET技术,详细述说ADO超时相关问题介绍
- 网页防删改系统实现的相关技术
- How to Implement Add/Edit/Delete/View with PHP using Angular JS (Part-2)
- 虚拟化相关概念与技术整理(2)——硬件辅助虚拟化简介
- 淘宝开源Key/Value结构数据存储系统Tair技术剖析
- TEC1304.值集和弹性域相关技术总结 - 第三章 键弹性域详解
- JEECMS源代码基本结构及相关技术简介
- setValuesForKeysWithDictionary forUndefinedKey
- laravel migrate 时出现错误 [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key