DataTableExtensions.CopyToDataTable DataTableExtensions.CopyToDataTable DataTableExtensions.CopyToDataTable DataTableExtensions.CopyToDataTable Method

定义

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

重载

CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) 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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) 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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) 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>) CopyToDataTable<T>(IEnumerable<T>) CopyToDataTable<T>(IEnumerable<T>) 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.

参数

返回

一个 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 表中的订单, CopyToDataTable并使用方法DataTable从该查询创建。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>源, 不仅可以是查询, 也可以是查询。 DataTableThe 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.

目标表的架构基于源序列中第DataRow一行的架构。The schema of the destination table is based on the schema of the first DataRow row in the source sequence. 表元数据从的DataRow元数据和表值中DataRow提取。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. 在从源复制到RowError返回RowStateDataTable过程中, 不会保留和属性。 DataRowThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

DataTable ValueDataRow中找到值为null的空引用或可为 null 的类型时, 此方法会将目标中的值替换为的值。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、将其附加DataSet到或执行任何其他DataTable特定操作, 则必须在创建并返回表后执行此操作。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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) 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.

参数

table
DataTable DataTable DataTable DataTable

目标 DataTableThe destination DataTable.

options
LoadOption LoadOption LoadOption 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>源, 而不仅是对的DataTable查询。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.

目标表的架构基于源序列中第DataRow一行的架构。The schema of the destination table is based on the schema of the first DataRow row in the source sequence. 表元数据从的DataRow元数据和表值中DataRow提取。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. 在从源复制到RowError返回RowStateDataTable过程中, 不会保留和属性。 DataRowThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

DataTable ValueDataRow中找到值为null的空引用或可为 null 的类型时, 此方法会将目标中的值替换为的值。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、将其附加DataSet到或执行任何其他DataTable特定操作, 则必须在创建并返回表后执行此操作。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) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) 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.

参数

table
DataTable DataTable DataTable DataTable

目标 DataTableThe destination DataTable.

options
LoadOption LoadOption LoadOption LoadOption

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

errorHandler
FillErrorEventHandler FillErrorEventHandler FillErrorEventHandler 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>源, 而不仅是对的DataTable查询。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.

目标表的架构基于源序列中第DataRow一行的架构。The schema of the destination table is based on the schema of the first DataRow row in the source sequence. 表元数据从的DataRow元数据和表值中DataRow提取。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. 在从源复制到RowError返回RowStateDataTable过程中, 不会保留和属性。 DataRowThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

DataTable ValueDataRow中找到值为null的空引用或可为 null 的类型时, 此方法会将目标中的值替换为的值。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、将其附加DataSet到或执行任何其他DataTable特定操作, 则必须在创建并返回表后执行此操作。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. 使用以下值将传递errorHandler给委托: FillErrorEventArgsA FillErrorEventArgs is passed to the errorHandler delegate with the following values:

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

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

适用于