DataTableExtensions.CopyToDataTable 方法

定义

在给定输入 DataTable 对象的情况下,返回包含 DataRow 对象副本的 IEnumerable<T>Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object.

重载

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRowReturns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object where the generic parameter T is DataRow.

public:
generic <typename T>
 where T : System::Data::DataRow[System::Runtime::CompilerServices::Extension]
 static System::Data::DataTable ^ CopyToDataTable(System::Collections::Generic::IEnumerable<T> ^ source);
public static System.Data.DataTable CopyToDataTable<T> (this System.Collections.Generic.IEnumerable<T> source) where T : System.Data.DataRow;
static member CopyToDataTable : seq<'T (requires 'T :> System.Data.DataRow)> -> System.Data.DataTable (requires 'T :> System.Data.DataRow)
<Extension()>
Public Function CopyToDataTable(Of T As DataRow) (source As IEnumerable(Of T)) As DataTable

类型参数

T

源序列中的对象类型(通常为 DataRow)。The type of objects in the source sequence, typically DataRow.

参数

source
IEnumerable<T>

IEnumerable<T> 序列。The source IEnumerable<T> sequence.

返回

一个 DataTable,其中包含作为 DataRow 对象的类型的输入序列。A DataTable that contains the input sequence as the type of DataRow objects.

异常

IEnumerable<T> 序列为 null,因此无法创建新表。The source IEnumerable<T> sequence is null and a new table cannot be created.

源序列中的 DataRow 的状态为 DeletedA DataRow in the source sequence has a state of Deleted.

源序列不包含任何 DataRow 对象。The source sequence does not contain any DataRow objects.

源序列中的 DataRownullA DataRow in the source sequence is null.

示例

下面的示例在2001年8月8日之后查询 SalesOrderHeader 表中的订单,并使用 @no__t 0 方法从该查询创建 @no__t 1。The following example queries the SalesOrderHeader table for orders after August 8, 2001, and uses the CopyToDataTable method to create a DataTable from that query. 然后将 DataTable 绑定到作为 BindingSource 的代理的 DataGridViewThe DataTable is then bound to a BindingSource, which acts as proxy for a DataGridView.

// Bind the System.Windows.Forms.DataGridView object
// to the System.Windows.Forms.BindingSource object.
dataGridView.DataSource = bindingSource;

// Fill the DataSet.
DataSet ds = new DataSet();
ds.Locale = CultureInfo.InvariantCulture;
FillDataSet(ds);

DataTable orders = ds.Tables["SalesOrderHeader"];

// Query the SalesOrderHeader table for orders placed 
// after August 8, 2001.
IEnumerable<DataRow> query =
    from order in orders.AsEnumerable()
    where order.Field<DateTime>("OrderDate") > new DateTime(2001, 8, 1)
    select order;

// Create a table from the query.
DataTable boundTable = query.CopyToDataTable<DataRow>();

// Bind the table to a System.Windows.Forms.BindingSource object, 
// which acts as a proxy for a System.Windows.Forms.DataGridView object.
bindingSource.DataSource = boundTable;
' Bind the System.Windows.Forms.DataGridView object
' to the System.Windows.Forms.BindingSource object.
dataGridView.DataSource = bindingSource

' Fill the DataSet.
Dim ds As New DataSet()
ds.Locale = CultureInfo.InvariantCulture
' See the FillDataSet method in the Loading Data Into a DataSet topic.
FillDataSet(ds)

Dim orders As DataTable = ds.Tables("SalesOrderHeader")

' Query the SalesOrderHeader table for orders placed 
'  after August 8, 2001.
Dim query = _
    From order In orders.AsEnumerable() _
    Where order.Field(Of DateTime)("OrderDate") > New DateTime(2001, 8, 1) _
    Select order

' Create a table from the query.
Dim boundTable As DataTable = query.CopyToDataTable()

' Bind the table to a System.Windows.Forms.BindingSource object, 
' which acts as a proxy for a System.Windows.Forms.DataGridView object.
bindingSource.DataSource = boundTable

注解

输入参数 T 的参数 source 的类型只能是 DataRow 或从 DataRow 派生的类型。The parameter T of the input parameter source can only be of type DataRow or a type derived from DataRow.

输入序列可以是任何 IEnumerable<T> 源,而不仅是对 @no__t 的查询或查询。The input sequence can be any IEnumerable<T> source, not only a query or a query over a DataTable. 请注意,如果源序列是查询,则调用此运算符将强制立即执行。Note that if the source sequence is a query, calling this operator will force immediate execution. 枚举源序列也会导致引发异常。Enumerating the source sequence can also cause exceptions to be thrown.

目标表的架构基于源序列中第一个 @no__t 0 行的架构。The schema of the destination table is based on the schema of the first DataRow row in the source sequence. 表元数据从 @no__t 的列值 @no__t 的元数据和表值提取而来。The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. 对于类型化的 DataTable,不保留类型。For a typed DataTable, types are not preserved. 传输数据和架构,但是输出表的结果行不属于类型化 DataRow 类型。The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. 从源 DataRow 到返回的 DataTable 的复制过程中,不会保留 RowState 和 @no__t 属性。The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在源 @no__t 中找到值为null引用或可为 null 的类型时,此方法会将目标 DataTable 中的值替换为 Value 的值。When a null reference or nullable type with a value of null is found in a source DataRow, this method replaces the value in the destination DataTable with a value of Value.

方法返回的生成表具有默认的表名称。The generated table returned by the method has the default table name. 如果要命名 DataTable,将其附加到 @no__t,或者执行任何其他特定于 @no__t 2 的操作,则必须在创建并返回表后执行此操作。If you want to name the DataTable, attach it to a DataSet, or perform any other DataTable specific operation, you must do so after the table is created and returned.

有关详细信息,请参阅从查询创建 DataTableFor more information, see Creating a DataTable From a Query.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

public:
generic <typename T>
 where T : System::Data::DataRow[System::Runtime::CompilerServices::Extension]
 static void CopyToDataTable(System::Collections::Generic::IEnumerable<T> ^ source, System::Data::DataTable ^ table, System::Data::LoadOption options);
public static void CopyToDataTable<T> (this System.Collections.Generic.IEnumerable<T> source, System.Data.DataTable table, System.Data.LoadOption options) where T : System.Data.DataRow;
static member CopyToDataTable : seq<'T (requires 'T :> System.Data.DataRow)> * System.Data.DataTable * System.Data.LoadOption -> unit (requires 'T :> System.Data.DataRow)
<Extension()>
Public Sub CopyToDataTable(Of T As DataRow) (source As IEnumerable(Of T), table As DataTable, options As LoadOption)

类型参数

T

源序列中的对象类型(通常为 DataRow)。The type of objects in the source sequence, typically DataRow.

参数

source
IEnumerable<T>

IEnumerable<T> 序列。The source IEnumerable<T> sequence.

table
DataTable

目标 DataTableThe destination DataTable.

options
LoadOption

LoadOption 枚举,用于指定 DataTable 加载选项。A LoadOption enumeration that specifies the DataTable load options.

异常

复制的 DataRow 对象不符合目标 DataTable 的架构。The copied DataRow objects do not fit the schema of the destination DataTable.

IEnumerable<T> 序列为 null,或者目标 DataTablenullThe source IEnumerable<T> sequence is null or the destination DataTable is null.

源序列中的 DataRow 的状态为 DeletedA DataRow in the source sequence has a state of Deleted.

源序列不包含任何 DataRow 对象。The source sequence does not contain any DataRow objects.

源序列中的 DataRownullA DataRow in the source sequence is null.

注解

输入参数 T 的参数 source 的类型只能是 DataRow 或从 DataRow 派生的类型。The parameter T of the input parameter source can only be of type DataRow, or a type derived from DataRow.

输入序列可以是任何 IEnumerable<T> 源,而不仅是对 @no__t 的查询。The input sequence can be any IEnumerable<T> source, not only a query over a DataTable. 请注意,如果源序列是查询,则调用此运算符将强制立即执行。Note that if the source sequence is a query, calling this operator will force immediate execution. 枚举源序列也会导致引发异常。Enumerating the source sequence can also cause exceptions to be thrown.

目标表的架构基于源序列中第一个 @no__t 0 行的架构。The schema of the destination table is based on the schema of the first DataRow row in the source sequence. 表元数据从 @no__t 的列值 @no__t 的元数据和表值提取而来。The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. 对于类型化的 DataTable,不保留类型。For a typed DataTable, types are not preserved. 传输数据和架构,但是输出表的结果行不属于类型化 DataRow 类型。The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. 从源 DataRow 到返回的 DataTable 的复制过程中,不会保留 RowState 和 @no__t 属性。The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在源 @no__t 中找到值为null引用或可为 null 的类型时,此方法会将目标 DataTable 中的值替换为 Value 的值。When a null reference or nullable type with a value of null is found in a source DataRow, this method replaces the value in the destination DataTable with a value of Value.

方法返回的生成表具有默认的表名称。The generated table returned by the method has the default table name. 如果要命名 DataTable,将其附加到 @no__t,或者执行任何其他特定于 @no__t 2 的操作,则必须在创建并返回表后执行此操作。If you want to name the DataTable, attach it to a DataSet, or perform any other DataTable specific operation, you must do so after the table is created and returned.

有关详细信息,请参阅从查询创建 DataTableFor more information, see Creating a DataTable From a Query.

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRowCopies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

public:
generic <typename T>
 where T : System::Data::DataRow[System::Runtime::CompilerServices::Extension]
 static void CopyToDataTable(System::Collections::Generic::IEnumerable<T> ^ source, System::Data::DataTable ^ table, System::Data::LoadOption options, System::Data::FillErrorEventHandler ^ errorHandler);
public static void CopyToDataTable<T> (this System.Collections.Generic.IEnumerable<T> source, System.Data.DataTable table, System.Data.LoadOption options, System.Data.FillErrorEventHandler errorHandler) where T : System.Data.DataRow;
static member CopyToDataTable : seq<'T (requires 'T :> System.Data.DataRow)> * System.Data.DataTable * System.Data.LoadOption * System.Data.FillErrorEventHandler -> unit (requires 'T :> System.Data.DataRow)
<Extension()>
Public Sub CopyToDataTable(Of T As DataRow) (source As IEnumerable(Of T), table As DataTable, options As LoadOption, errorHandler As FillErrorEventHandler)

类型参数

T

源序列中的对象类型(通常为 DataRow)。The type of objects in the source sequence, typically DataRow.

参数

source
IEnumerable<T>

IEnumerable<T> 序列。The source IEnumerable<T> sequence.

table
DataTable

目标 DataTableThe destination DataTable.

options
LoadOption

LoadOption 枚举,用于指定 DataTable 加载选项。A LoadOption enumeration that specifies the DataTable load options.

errorHandler
FillErrorEventHandler

FillErrorEventHandler 委托,表示将处理错误的方法。A FillErrorEventHandler delegate that represents the method that will handle an error.

异常

复制的 DataRow 对象不符合目标 DataTable 的架构。The copied DataRow objects do not fit the schema of the destination DataTable.

IEnumerable<T> 序列为 null,或者目标 DataTablenullThe source IEnumerable<T> sequence is null or the destination DataTable is null.

源序列中的 DataRow 的状态为 DeletedA DataRow in the source sequence has a state of Deleted.

-or- 源序列不包含任何 DataRow 对象。The source sequence does not contain any DataRow objects.

-or- 源序列中的 DataRownullA DataRow in the source sequence is null.

注解

输入参数 T 的参数 source 的类型只能是 DataRow 或从 DataRow 派生的类型。The parameter T of the input parameter source can only be of type DataRow, or a type derived from DataRow.

输入序列可以是任何 IEnumerable<T> 源,而不仅是对 @no__t 的查询。The input sequence can be any IEnumerable<T> source, not only a query over a DataTable. 请注意,如果源序列是查询,则调用此运算符将强制立即执行。Note that if the source sequence is a query, calling this operator will force immediate execution. 枚举源序列也会导致引发异常。Enumerating the source sequence can also cause exceptions to be thrown.

目标表的架构基于源序列中第一个 @no__t 0 行的架构。The schema of the destination table is based on the schema of the first DataRow row in the source sequence. 表元数据从 @no__t 的列值 @no__t 的元数据和表值提取而来。The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. 对于类型化的 DataTable,不保留类型。For a typed DataTable, types are not preserved. 传输数据和架构,但是输出表的结果行不属于类型化 DataRow 类型。The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. 从源 DataRow 到返回的 DataTable 的复制过程中,不会保留 RowState 和 @no__t 属性。The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在源 @no__t 中找到值为null引用或可为 null 的类型时,此方法会将目标 DataTable 中的值替换为 Value 的值。When a null reference or nullable type with a value of null is found in a source DataRow, this method replaces the value in the destination DataTable with a value of Value.

方法返回的生成表具有默认的表名称。The generated table returned by the method has the default table name. 如果要命名 DataTable,将其附加到 @no__t,或者执行任何其他特定于 @no__t 2 的操作,则必须在创建并返回表后执行此操作。If you want to name the DataTable, attach it to a DataSet, or perform any other DataTable specific operation, you must do so after the table is created and returned.

如果在将数据行复制到目标表中时引发了异常,如约束异常,则调用 errorHandler 委托。If an exception is thrown during the copy of a data row into the target table, such as a constraint exception, the errorHandler delegate is called. 使用以下值将 @no__t 0 传递到 errorHandler 委托:A FillErrorEventArgs is passed to the errorHandler delegate with the following values:

  • @No__t-0 属性设置为源数据的副本。The Values property is set to a copy of the source data.

  • @No__t 的 @no__t 属性设置为目标。The DataTable property is set to the target DataTable.

  • @No__t-0 属性设置为捕获的异常。The Errors property is set to the caught exception.

委托调用返回后,将读取 Continue 属性。The Continue property is read after the delegate call returns. 如果 Continue 属性 @no__t 为-1,则继续枚举源序列并将其加载到数据表。If the Continue property is true, the source sequence continues to be enumerated and loaded into the data table. 如果 Continue 属性 @no__t 为-1,则将从 CopyToDataTable 方法引发原始异常。If the Continue property is false, the original exception is thrown from the CopyToDataTable method.

有关详细信息,请参阅从查询创建 DataTableFor more information, see Creating a DataTable From a Query.

适用于