DataTableExtensions.CopyToDataTable 方法

定义

在给定输入 DataTable 对象的情况下,返回包含 DataRow 对象副本的 IEnumerable<T>

重载

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 DataRow

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

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRow

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

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 DataRow

CopyToDataTable<T>(IEnumerable<T>)

在给定其泛型参数 TDataTable 的输入 DataRow 对象的情况下,返回包含 IEnumerable<T> 对象副本的 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)。

参数

source
IEnumerable<T>

IEnumerable<T> 序列。

返回

一个 DataTable,其中包含作为 DataRow 对象的类型的输入序列。

例外

IEnumerable<T> 序列为 null,因此无法创建新表。

源序列中的 DataRow 的状态为 Deleted

源序列不包含任何 DataRow 对象。

源序列中的 DataRownull

示例

以下示例查询 SalesOrderHeader 表以获取 2001 年 8 月 8 日之后的订单,并使用 CopyToDataTable 方法从该查询创建 DataTable 。 然后将 DataTable 绑定到作为 BindingSource 的代理的 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

注解

输入参数Tsource的参数只能属于 类型DataRow或派生自 DataRow的类型。

输入序列可以是任何 IEnumerable<T> 源,而不仅仅是查询或对 的 DataTable查询。 请注意,如果源序列是查询,则调用此运算符将强制立即执行。 枚举源序列也可能导致引发异常。

目标表的架构基于源序列中第一 DataRow 行的架构。 表元数据是从 元数据中提取的, DataRow 而表值则从 的 DataRow列值中提取。 对于类型化 DataTable,不会保留类型。 传输数据和架构,但输出表的结果行将不是类型化 DataRow 类型。 在RowState从源DataRow复制到返回DataTable的 期间,不会保留 和 RowError 属性。

当在源 DataRow中找到值为 null 的 null 引用或可以为 null 的类型时,此方法会将目标DataTable中的值替换为值 Value

方法返回的生成的表具有默认表名。 如果要命名 、 DataTable将其附加到 DataSet或执行任何其他 DataTable 特定操作,则必须在创建并返回表后执行此操作。

有关详细信息,请参阅 从查询创建 DataTable

适用于

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

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 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)。

参数

source
IEnumerable<T>

IEnumerable<T> 序列。

table
DataTable

目标 DataTable

options
LoadOption

LoadOption 枚举,用于指定 DataTable 加载选项。

例外

复制的 DataRow 对象不符合目标 DataTable 的架构。

IEnumerable<T> 序列为 null,或者目标 DataTablenull

源序列中的 DataRow 的状态为 Deleted

源序列不包含任何 DataRow 对象。

源序列中的 DataRownull

注解

输入参数 T 的 参数 source 只能属于 类型 DataRow或派生自 DataRow的类型。

输入序列可以是任何 IEnumerable<T> 源,而不仅仅是对 的 DataTable查询。 请注意,如果源序列是查询,则调用此运算符将强制立即执行。 枚举源序列也可能导致引发异常。

目标表的架构基于源序列中第一 DataRow 行的架构。 表元数据是从 元数据中提取的, DataRow 而表值则从 的 DataRow列值中提取。 对于类型化 DataTable,不会保留类型。 传输数据和架构,但输出表的结果行将不是类型化 DataRow 类型。 在RowState从源DataRow复制到返回DataTable的 期间,不会保留 和 RowError 属性。

当在源 DataRow中找到值为 null 的 null 引用或可以为 null 的类型时,此方法会将目标DataTable中的值替换为值 Value

方法返回的生成的表具有默认表名。 如果要命名 、 DataTable将其附加到 DataSet或执行任何其他 DataTable 特定操作,则必须在创建并返回表后执行此操作。

有关详细信息,请参阅 从查询创建 DataTable

适用于

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

在给定其泛型参数 TDataRow 的输入 DataTable 对象的情况下,将 IEnumerable<T> 对象复制到指定的 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)。

参数

source
IEnumerable<T>

IEnumerable<T> 序列。

table
DataTable

目标 DataTable

options
LoadOption

LoadOption 枚举,用于指定 DataTable 加载选项。

errorHandler
FillErrorEventHandler

FillErrorEventHandler 委托,表示将处理错误的方法。

例外

复制的 DataRow 对象不符合目标 DataTable 的架构。

IEnumerable<T> 序列为 null,或者目标 DataTablenull

源序列中的 DataRow 的状态为 Deleted

- 或 -

源序列不包含任何 DataRow 对象。

- 或 -

源序列中的 DataRownull

注解

输入参数 T 的 参数 source 只能属于 类型 DataRow或派生自 DataRow的类型。

输入序列可以是任何 IEnumerable<T> 源,而不仅仅是对 的 DataTable查询。 请注意,如果源序列是查询,则调用此运算符将强制立即执行。 枚举源序列也可能导致引发异常。

目标表的架构基于源序列中第一 DataRow 行的架构。 表元数据是从 元数据中提取的, DataRow 而表值则从 的 DataRow列值中提取。 对于类型化 DataTable,不会保留类型。 传输数据和架构,但输出表的结果行将不是类型化 DataRow 类型。 在RowState从源DataRow复制到返回DataTable的 期间,不会保留 和 RowError 属性。

当在源 DataRow中找到值为 null 的 null 引用或可以为 null 的类型时,此方法会将目标DataTable中的值替换为值 Value

方法返回的生成的表具有默认表名。 如果要命名 、 DataTable将其附加到 DataSet或执行任何其他 DataTable 特定操作,则必须在创建并返回表后执行此操作。

如果在将数据行复制到目标表中期间引发异常(例如约束异常),则 errorHandler 调用委托。 FillErrorEventArgs将 传递给errorHandler具有以下值的委托:

Continue 属性在委托调用返回后读取。 Continue如果 属性为 true,则继续枚举源序列并将其加载到数据表中。 Continue如果 属性为 false,则从 CopyToDataTable 方法引发原始异常。

有关详细信息,请参阅 从查询创建 DataTable

适用于