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

一个 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 或派生自的类型 DataRowThe 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 元数据和表值中提取 DataRowThe 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. RowState RowError 从源复制到返回的过程中,不会保留和属性 DataRow DataTableThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在源中找到值为null引用或可为 null 的类型时 DataRow ,此方法会将目标中的值替换为 DataTable 的值 ValueWhen 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)

在给定其泛型参数 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 或派生自的类型 DataRowThe 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 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 元数据和表值中提取 DataRowThe 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. RowState RowError 从源复制到返回的过程中,不会保留和属性 DataRow DataTableThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在源中找到值为null引用或可为 null 的类型时 DataRow ,此方法会将目标中的值替换为 DataTable 的值 ValueWhen 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)

在给定其泛型参数 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;
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 或派生自的类型 DataRowThe 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 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 元数据和表值中提取 DataRowThe 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. RowState RowError 从源复制到返回的过程中,不会保留和属性 DataRow DataTableThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在源中找到值为null引用或可为 null 的类型时 DataRow ,此方法会将目标中的值替换为 DataTable 的值 ValueWhen 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. FillErrorEventArgs errorHandler 使用以下值将传递给委托:A FillErrorEventArgs is passed to the errorHandler delegate with the following values:

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

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

适用于