DataSet.Load 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
DataSet使用提供的IDataReader数据源中的值填充值。
重载
| 名称 | 说明 |
|---|---|
| Load(IDataReader, LoadOption, DataTable[]) |
DataSet使用提供的、实例数组DataTable来提供IDataReader架构和命名空间信息,使用数据源中的值填充值。 |
| Load(IDataReader, LoadOption, String[]) |
使用提供的IDataReader数据源中的值填充值DataSet,并使用字符串数组来提供表中 |
| Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[]) |
DataSet使用提供的、实例数组DataTable来提供IDataReader架构和命名空间信息,使用数据源中的值填充值。 |
注解
该方法Load提供了一种使用从IDataReader实例检索的数据填充单个DataTable的方法。 此方法提供相同的功能,但允许您将多个结果集从一个 IDataReader 结果集加载到一个 DataSet表中。
DataSet如果已包含行,则数据源中的传入数据将与现有行合并。
该方法 Load 可用于几种常见方案,这些方案都集中在从指定数据源获取数据并将其添加到当前数据容器(在本例中为 a DataSet)。 这些方案描述了其 DataSet更新和合并行为的标准用法。
与 DataSet 单个主数据源同步或更新。 跟踪 DataSet 更改,允许与主数据源同步。 此外,还可以 DataSet 接受来自一个或多个辅助数据源的增量数据。 不 DataSet 负责跟踪更改,以便允许与辅助数据源同步。
鉴于这两个假设数据源,用户可能需要以下行为之一:
从主数据源初始化
DataSet。 在此方案中,用户希望使用主数据源中的值初始化空DataSet。 修改了一个或多个 DataTable 的内容。 稍后,用户打算将更改传播回主数据源。保留更改并从主数据源重新同步。 在此方案中,用户希望获取
DataSet上一个方案中的填充内容,并执行与主数据源的增量同步,从而保留对主DataSet数据源所做的修改。辅助数据源中的增量数据馈送。 在此方案中,用户希望合并来自一个或多个辅助数据源的更改,并将这些更改传播回主数据源。
该方法 Load 使所有这些方案成为可能。 此方法允许你指定加载选项参数,该参数指示合并中的 DataTable 行与正在加载的行的方式。 下表描述了枚举提供的三个 LoadOption 加载选项。 在每个情况下,说明指示传入数据中某一行的主键与现有行的主键匹配时的行为。
| 加载选项 | 说明 |
|---|---|
PreserveChanges(默认值) |
使用传入行的值更新行的原始版本。 |
OverwriteChanges |
使用传入行的值更新行的当前版本和原始版本。 |
Upsert |
使用传入行的值更新行的当前版本。 |
一般情况下,和PreserveChangesOverwriteChanges选项适用于用户需要将更改与其主要数据源同步DataSet的情况。 该 Upsert 选项有助于聚合来自一个或多个辅助数据源的更改。
Load(IDataReader, LoadOption, DataTable[])
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
DataSet使用提供的、实例数组DataTable来提供IDataReader架构和命名空间信息,使用数据源中的值填充值。
public:
void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, ... cli::array <System::Data::DataTable ^> ^ tables);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")]
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params System.Data.DataTable[] tables);
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params System.Data.DataTable[] tables);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")>]
member this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.DataTable[] -> unit
member this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.DataTable[] -> unit
Public Sub Load (reader As IDataReader, loadOption As LoadOption, ParamArray tables As DataTable())
参数
- reader
- IDataReader
IDataReader一个提供一个或多个结果集。
- loadOption
- LoadOption
枚举中的LoadOption一个值,该值指示如何将实例中的DataTableDataSet行与共享同一主键的传入行组合在一起。
- tables
- DataTable[]
方法从Load(IDataReader, LoadOption, DataTable[])中检索名称和命名空间信息的实例数组DataTable。 这些表中的每个表都必须是包含的一DataSet个成员DataTableCollection。
- 属性
示例
以下示例创建一个新 DataSet实例,将两 DataTable 个实例添加到 DataSet其中,然后填充 DataSet 该方法 Load ,从包含两个 DataTableReader 结果集的数据检索数据。 最后,该示例在控制台窗口中显示表的内容。
static void Main()
{
DataSet dataSet = new DataSet();
DataTable customerTable = new DataTable();
DataTable productTable = new DataTable();
// This information is cosmetic, only.
customerTable.TableName = "Customers";
productTable.TableName = "Products";
// Add the tables to the DataSet:
dataSet.Tables.Add(customerTable);
dataSet.Tables.Add(productTable);
// Load the data into the existing DataSet.
DataTableReader reader = GetReader();
dataSet.Load(reader, LoadOption.OverwriteChanges,
customerTable, productTable);
// Print out the contents of each table:
foreach (DataTable table in dataSet.Tables)
{
PrintColumns(table);
}
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static DataTable GetCustomers()
{
// Create sample Customers table.
DataTable table = new DataTable();
table.TableName = "Customers";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Mary" });
table.Rows.Add(new object[] { 1, "Andy" });
table.Rows.Add(new object[] { 2, "Peter" });
table.AcceptChanges();
return table;
}
private static DataTable GetProducts()
{
// Create sample Products table.
DataTable table = new DataTable();
table.TableName = "Products";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID",
typeof(int));
table.Columns.Add("Name", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Wireless Network Card" });
table.Rows.Add(new object[] { 1, "Hard Drive" });
table.Rows.Add(new object[] { 2, "Monitor" });
table.Rows.Add(new object[] { 3, "CPU" });
table.AcceptChanges();
return table;
}
private static void PrintColumns(DataTable table)
{
Console.WriteLine();
Console.WriteLine(table.TableName);
Console.WriteLine("=========================");
// Loop through all the rows in the table:
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}
}
private static DataTableReader GetReader()
{
// Return a DataTableReader containing multiple
// result sets, just for the sake of this demo.
DataSet dataSet = new DataSet();
dataSet.Tables.Add(GetCustomers());
dataSet.Tables.Add(GetProducts());
return dataSet.CreateDataReader();
}
Sub Main()
Dim dataSet As New DataSet
Dim customerTable As New DataTable
Dim productTable As New DataTable
' This information is cosmetic, only.
customerTable.TableName = "Customers"
productTable.TableName = "Products"
' Add the tables to the DataSet:
dataSet.Tables.Add(customerTable)
dataSet.Tables.Add(productTable)
' Load the data into the existing DataSet.
Dim reader As DataTableReader = GetReader()
dataSet.Load(reader, LoadOption.OverwriteChanges, _
customerTable, productTable)
' Print out the contents of each table:
For Each table As DataTable In dataSet.Tables
PrintColumns(table)
Next
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Function GetCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
table.TableName = "Customers"
' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(Integer))
table.Columns.Add("Name", GetType(String))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {0, "Mary"})
table.Rows.Add(New Object() {1, "Andy"})
table.Rows.Add(New Object() {2, "Peter"})
table.AcceptChanges()
Return table
End Function
Private Function GetProducts() As DataTable
' Create sample Products table, in order
' to demonstrate the behavior of the DataTableReader.
Dim table As New DataTable
table.TableName = "Products"
' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(Integer))
table.Columns.Add("Name", GetType(String))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {0, "Wireless Network Card"})
table.Rows.Add(New Object() {1, "Hard Drive"})
table.Rows.Add(New Object() {2, "Monitor"})
table.Rows.Add(New Object() {3, "CPU"})
Return table
End Function
Private Function GetReader() As DataTableReader
' Return a DataTableReader containing multiple
' result sets, just for the sake of this demo.
Dim dataSet As New DataSet
dataSet.Tables.Add(GetCustomers())
dataSet.Tables.Add(GetProducts())
Return dataSet.CreateDataReader()
End Function
Private Sub PrintColumns( _
ByVal table As DataTable)
Console.WriteLine()
Console.WriteLine(table.TableName)
Console.WriteLine("=========================")
' Loop through all the rows in the table.
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
Console.Write(row(col).ToString() & " ")
Next
Console.WriteLine()
Next
End Sub
注解
该方法Load提供了一种使用从IDataReader实例检索的数据填充单个DataTable的方法。 此方法提供相同的功能,但允许您将多个结果集从一个 IDataReader 结果集加载到一个 DataSet表中。
注释
如果传入reader的任何源数据列都是计算列,则加载操作将失败InvalidOperationException。
该 loadOption 参数允许指定导入的数据与现有数据交互的方式,并且可以是枚举中的任何 LoadOption 值。 有关使用此参数的详细信息,请参阅该方法的文档 DataTableLoad 。
该 tables 参数允许指定实例数组 DataTable ,指示与从读取器加载的每个结果集对应的表的顺序。 该方法 Load 使用源数据读取器中单个结果集中的数据填充每个提供的 DataTable 实例。 在每个结果集之后, Load 该方法将转到读取器中的下一个结果集,直到没有其他结果集。
此方法的名称解析方案与类的方法DbDataAdapter后跟Fill相同。
另请参阅
适用于
Load(IDataReader, LoadOption, String[])
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
使用提供的IDataReader数据源中的值填充值DataSet,并使用字符串数组来提供表中DataSet的名称。
public:
void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, ... cli::array <System::String ^> ^ tables);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")]
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params string[] tables);
public void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, params string[] tables);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")>]
member this.Load : System.Data.IDataReader * System.Data.LoadOption * string[] -> unit
member this.Load : System.Data.IDataReader * System.Data.LoadOption * string[] -> unit
Public Sub Load (reader As IDataReader, loadOption As LoadOption, ParamArray tables As String())
参数
- reader
- IDataReader
IDataReader一个提供一个或多个结果集。
- loadOption
- LoadOption
枚举中的LoadOption一个值,该值指示如何将实例中的DataTableDataSet行与共享同一主键的传入行组合在一起。
- tables
- String[]
一个字符串数组,该方法 Load 从中检索表名信息。
- 属性
示例
以下控制台应用程序示例首先使用Load该方法创建表并将读取器中的数据加载到 aDataSet。 然后,该示例向表 DataSet 添加表,并尝试用数据 DataTableReader填充表。 在此示例中,由于传递给 Load 该方法的参数指示不存在的表名,因此该方法 Load 将创建一个新表以匹配作为参数传递的名称。 加载数据后,该示例将在控制台窗口中显示其所有表的内容。
static void Main()
{
DataSet dataSet = new DataSet();
DataTableReader reader = GetReader();
// The tables listed as parameters for the Load method
// should be in the same order as the tables within the IDataReader.
dataSet.Load(reader, LoadOption.Upsert, "Customers", "Products");
foreach (DataTable table in dataSet.Tables)
{
PrintColumns(table);
}
// Now try the example with the DataSet
// already filled with data:
dataSet = new DataSet();
dataSet.Tables.Add(GetCustomers());
dataSet.Tables.Add(GetProducts());
// Retrieve a data reader containing changed data:
reader = GetReader();
// Load the data into the existing DataSet. Retrieve the order of the
// the data in the reader from the
// list of table names in the parameters. If you specify
// a new table name here, the Load method will create
// a corresponding new table.
dataSet.Load(reader, LoadOption.Upsert,
"NewCustomers", "Products");
foreach (DataTable table in dataSet.Tables)
{
PrintColumns(table);
}
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static DataTable GetCustomers()
{
// Create sample Customers table.
DataTable table = new DataTable();
table.TableName = "Customers";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Mary" });
table.Rows.Add(new object[] { 1, "Andy" });
table.Rows.Add(new object[] { 2, "Peter" });
table.AcceptChanges();
return table;
}
private static DataTable GetProducts()
{
// Create sample Products table.
DataTable table = new DataTable();
table.TableName = "Products";
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 0, "Wireless Network Card" });
table.Rows.Add(new object[] { 1, "Hard Drive" });
table.Rows.Add(new object[] { 2, "Monitor" });
table.Rows.Add(new object[] { 3, "CPU" });
table.AcceptChanges();
return table;
}
private static void PrintColumns(DataTable table)
{
Console.WriteLine();
Console.WriteLine(table.TableName);
Console.WriteLine("=========================");
// Loop through all the rows in the table:
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
Console.Write(row[i] + " ");
}
Console.WriteLine();
}
}
private static DataTableReader GetReader()
{
// Return a DataTableReader containing multiple
// result sets, just for the sake of this demo.
DataSet dataSet = new DataSet();
dataSet.Tables.Add(GetCustomers());
dataSet.Tables.Add(GetProducts());
return dataSet.CreateDataReader();
}
Sub Main()
Dim dataSet As New DataSet
Dim table As DataTable
Dim reader As DataTableReader = GetReader()
' The tables listed as parameters for the Load method
' should be in the same order as the tables within the IDataReader.
dataSet.Load(reader, LoadOption.Upsert, "Customers", "Products")
For Each table In dataSet.Tables
PrintColumns(table)
Next
' Now try the example with the DataSet
' already filled with data:
dataSet = New DataSet
dataSet.Tables.Add(GetCustomers())
dataSet.Tables.Add(GetProducts())
' Retrieve a data reader containing changed data:
reader = GetReader()
' Load the data into the existing DataSet. Retrieve the order of the
' the data in the reader from the
' list of table names in the parameters. If you specify
' a new table name here, the Load method will create
' a corresponding new table.
dataSet.Load(reader, LoadOption.Upsert, "NewCustomers", "Products")
For Each table In dataSet.Tables
PrintColumns(table)
Next
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Function GetCustomers() As DataTable
' Create sample Customers table.
Dim table As New DataTable
table.TableName = "Customers"
' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
table.Columns.Add("Name", GetType(String))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {0, "Mary"})
table.Rows.Add(New Object() {1, "Andy"})
table.Rows.Add(New Object() {2, "Peter"})
table.AcceptChanges()
Return table
End Function
Private Function GetProducts() As DataTable
' Create sample Products table, in order
' to demonstrate the behavior of the DataTableReader.
Dim table As New DataTable
table.TableName = "Products"
' Create two columns, ID and Name.
Dim idColumn As DataColumn = table.Columns.Add("ID", GetType(Integer))
table.Columns.Add("Name", GetType(String))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {0, "Wireless Network Card"})
table.Rows.Add(New Object() {1, "Hard Drive"})
table.Rows.Add(New Object() {2, "Monitor"})
table.Rows.Add(New Object() {3, "CPU"})
Return table
End Function
Private Function GetReader() As DataTableReader
' Return a DataTableReader containing multiple
' result sets, just for the sake of this demo.
Dim dataSet As New DataSet
dataSet.Tables.Add(GetCustomers())
dataSet.Tables.Add(GetProducts())
Return dataSet.CreateDataReader()
End Function
Private Sub PrintColumns( _
ByVal table As DataTable)
Console.WriteLine()
Console.WriteLine(table.TableName)
Console.WriteLine("=========================")
' Loop through all the rows in the table.
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
Console.Write(row(col).ToString() & " ")
Next
Console.WriteLine()
Next
End Sub
注解
该方法Load提供了一种使用从IDataReader实例检索的数据填充单个DataTable的方法。 此方法提供相同的功能,但允许您将多个结果集从一个 IDataReader 结果集加载到一个 DataSet表中。
注释
如果传入reader的任何源数据列都是计算列,则加载操作将失败InvalidOperationException。
该 loadOption 参数允许指定导入的数据与现有数据交互的方式,并且可以是枚举中的任何 LoadOption 值。 有关使用此参数的详细信息,请参阅该方法的文档 Load 。
该 tables 参数允许指定表名数组,指示与从读取器加载的每个结果集对应的表的顺序。 该方法 Load 尝试按顺序查找与表名数组中找到的名称匹配的表 DataSet 。 如果找到匹配的表,则会使用当前结果集的内容加载该表。 如果未找到匹配表,则使用表名称数组中提供的名称创建表,并从结果集中推断新表的架构。 在每个结果集之后, Load 该方法将转到读取器中的下一个结果集,直到没有其他结果集。
与 DataSet每个新创建的 DataTable命名空间关联的默认命名空间(如果有)。 此方法的名称解析方案与类的方法DbDataAdapter后跟Fill相同。
另请参阅
适用于
Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[])
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
- Source:
- DataSet.cs
DataSet使用提供的、实例数组DataTable来提供IDataReader架构和命名空间信息,使用数据源中的值填充值。
public:
virtual void Load(System::Data::IDataReader ^ reader, System::Data::LoadOption loadOption, System::Data::FillErrorEventHandler ^ errorHandler, ... cli::array <System::Data::DataTable ^> ^ tables);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")]
public virtual void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler? errorHandler, params System.Data.DataTable[] tables);
public virtual void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler? errorHandler, params System.Data.DataTable[] tables);
public virtual void Load(System.Data.IDataReader reader, System.Data.LoadOption loadOption, System.Data.FillErrorEventHandler errorHandler, params System.Data.DataTable[] tables);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Using LoadOption may cause members from types used in the expression column to be trimmed if not referenced directly.")>]
abstract member Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
override this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
abstract member Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
override this.Load : System.Data.IDataReader * System.Data.LoadOption * System.Data.FillErrorEventHandler * System.Data.DataTable[] -> unit
Public Overridable Sub Load (reader As IDataReader, loadOption As LoadOption, errorHandler As FillErrorEventHandler, ParamArray tables As DataTable())
参数
- reader
- IDataReader
IDataReader一个提供一个或多个结果集。
- loadOption
- LoadOption
枚举中的LoadOption一个值,该值指示如何将实例中的DataTableDataSet行与共享同一主键的传入行组合在一起。
- errorHandler
- FillErrorEventHandler
FillErrorEventHandler加载数据时发生错误时要调用的委托。
- tables
- DataTable[]
方法从Load(IDataReader, LoadOption, FillErrorEventHandler, DataTable[])中检索名称和命名空间信息的实例数组DataTable。
- 属性
示例
以下示例向 a DataSet添加一个表,然后尝试使用 Load 该方法从 DataTableReader 包含不兼容架构的数据加载数据。 此示例使用 FillErrorEventHandler 委托来调查和处理错误,而不是捕获错误。 输出显示在控制台窗口中。
static void Main()
{
// Attempt to load data from a data reader in which
// the schema is incompatible with the current schema.
// If you use exception handling, you won't get the chance
// to examine each row, and each individual table,
// as the Load method progresses.
// By taking advantage of the FillErrorEventHandler delegate,
// you can interact with the Load process as an error occurs,
// attempting to fix the problem, or simply continuing or quitting
// the Load process.:
DataSet dataSet = new DataSet();
DataTable table = GetIntegerTable();
dataSet.Tables.Add(table);
DataTableReader reader = new DataTableReader(GetStringTable());
dataSet.Load(reader, LoadOption.OverwriteChanges,
FillErrorHandler, table);
Console.WriteLine("Press any key to continue.");
Console.ReadKey();
}
private static DataTable GetIntegerTable()
{
// Create sample Customers table, in order
// to demonstrate the behavior of the DataTableReader.
DataTable table = new DataTable();
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(int));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { 4 });
table.Rows.Add(new object[] { 5 });
table.AcceptChanges();
return table;
}
private static DataTable GetStringTable()
{
// Create sample Customers table, in order
// to demonstrate the behavior of the DataTableReader.
DataTable table = new DataTable();
// Create two columns, ID and Name.
DataColumn idColumn = table.Columns.Add("ID", typeof(string));
// Set the ID column as the primary key column.
table.PrimaryKey = new DataColumn[] { idColumn };
table.Rows.Add(new object[] { "Mary" });
table.Rows.Add(new object[] { "Andy" });
table.Rows.Add(new object[] { "Peter" });
table.AcceptChanges();
return table;
}
static void FillErrorHandler(object sender, FillErrorEventArgs e)
{
// You can use the e.Errors value to determine exactly what
// went wrong.
if (e.Errors.GetType() == typeof(System.FormatException))
{
Console.WriteLine("Error when attempting to update the value: {0}",
e.Values[0]);
}
// Setting e.Continue to True tells the Load
// method to continue trying. Setting it to False
// indicates that an error has occurred, and the
// Load method raises the exception that got
// you here.
e.Continue = true;
}
Sub Main()
Dim dataSet As New DataSet
Dim table As New DataTable()
' Attempt to load data from a data reader in which
' the schema is incompatible with the current schema.
' If you use exception handling, you won't get the chance
' to examine each row, and each individual table,
' as the Load method progresses.
' By taking advantage of the FillErrorEventHandler delegate,
' you can interact with the Load process as an error occurs,
' attempting to fix the problem, or simply continuing or quitting
' the Load process.:
dataSet = New DataSet()
table = GetIntegerTable()
dataSet.Tables.Add(table)
Dim reader As New DataTableReader(GetStringTable())
dataSet.Load(reader, LoadOption.OverwriteChanges, _
AddressOf FillErrorHandler, table)
Console.WriteLine("Press any key to continue.")
Console.ReadKey()
End Sub
Private Sub FillErrorHandler(ByVal sender As Object, _
ByVal e As FillErrorEventArgs)
' You can use the e.Errors value to determine exactly what
' went wrong.
If e.Errors.GetType Is GetType(System.FormatException) Then
Console.WriteLine("Error when attempting to update the value: {0}", _
e.Values(0))
End If
' Setting e.Continue to True tells the Load
' method to continue trying. Setting it to False
' indicates that an error has occurred, and the
' Load method raises the exception that got
' you here.
e.Continue = True
End Sub
Private Function GetIntegerTable() As DataTable
' Create sample table with a single Int32 column.
Dim table As New DataTable
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(Integer))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {4})
table.Rows.Add(New Object() {5})
table.TableName = "IntegerTable"
table.AcceptChanges()
Return table
End Function
Private Function GetStringTable() As DataTable
' Create sample table with a single String column.
Dim table As New DataTable
Dim idColumn As DataColumn = table.Columns.Add("ID", _
GetType(String))
' Set the ID column as the primary key column.
table.PrimaryKey = New DataColumn() {idColumn}
table.Rows.Add(New Object() {"Mary"})
table.Rows.Add(New Object() {"Andy"})
table.Rows.Add(New Object() {"Peter"})
table.AcceptChanges()
Return table
End Function
Private Sub PrintColumns( _
ByVal table As DataTable)
' Loop through all the rows in the DataTableReader.
For Each row As DataRow In table.Rows
For Each col As DataColumn In table.Columns
Console.Write(row(col).ToString() & " ")
Next
Console.WriteLine()
Next
End Sub
注解
该方法Load提供了一种使用从IDataReader实例检索的数据填充单个DataTable的方法。 此方法提供相同的功能,但允许您将多个结果集从一个 IDataReader 结果集加载到一个 DataSet表中。
注释
如果传入reader的任何源数据列都是计算列,则加载操作将失败InvalidOperationException。
该 loadOption 参数允许指定导入的数据与现有数据交互的方式,并且可以是枚举中的任何 LoadOption 值。 有关使用此参数的详细信息,请参阅该方法的文档 DataTableLoad 。
该 errorHandler 参数是一个 FillErrorEventHandler 委托,它引用在加载数据时发生错误时调用的过程。
FillErrorEventArgs传递给过程的参数提供了属性,可用于检索有关所发生错误、当前数据行和DataTable正在填充的信息。 使用此委托机制(而不是更简单的 try/catch 块)可以确定错误、处理情况,以及根据需要继续处理。 参数 FillErrorEventArgs 提供一个 Continue 属性:将此属性设置为 true 指示已处理错误并想要继续处理;将属性设置为 false 指示要停止处理。 请注意,将属性设置为 false 导致触发问题的代码引发异常。
该 tables 参数允许指定实例数组 DataTable ,指示与从读取器加载的每个结果集对应的表的顺序。 该方法 Load 使用源数据读取器中单个结果集中的数据填充每个提供的 DataTable 实例。 在每个结果集之后, Load 该方法将转到读取器中的下一个结果集,直到没有其他结果集。
此方法的名称解析方案与类的方法DbDataAdapter后跟Fill相同。