DataTableExtensions.CopyToDataTable 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在给定输入 DataTable 对象的情况下,返回包含 DataRow 对象副本的 IEnumerable<T>。
重载
CopyToDataTable<T>(IEnumerable<T>) |
在给定其泛型参数 |
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption) |
在给定其泛型参数 |
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler) |
在给定其泛型参数 |
CopyToDataTable<T>(IEnumerable<T>)
在给定其泛型参数 T
为 DataTable 的输入 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
,因此无法创建新表。
示例
以下示例在 2001 年 8 月 8 日之后查询 SalesOrderHeader 表,并使用 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
注解
输入参数source
的参数T
只能的类型DataRow或派生自DataRow的类型。
输入序列可以是任何IEnumerable<T>源,而不仅仅是查询或查询。DataTable 请注意,如果源序列是查询,则调用此运算符将强制立即执行。 枚举源序列还可能导致引发异常。
目标表的架构基于源序列中第一 DataRow 行的架构。 表元数据是从 DataRow 元数据中提取的,而表值则从列 DataRow值中提取。 对于类型 DataTable化,不会保留类型。 传输数据和架构,但输出表的结果行不会属于类型化 DataRow 类型。 在RowState从源DataRow复制到返回DataTable的期间,不会保留这些属性和RowError属性。
当在源DataRow中找到值为 null 的 null 引用或可以为 null 的类型时,此方法将目标DataTable中的值替换为值Value。
方法返回的生成的表具有默认表名称。 如果要命名 DataTable、附加到 a DataSet或执行任何其他 DataTable 特定操作,则必须在创建并返回表后执行此操作。
有关详细信息,请参阅 从查询创建 DataTable。
适用于
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)
在给定其泛型参数 T
为 DataRow 的输入 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> 序列。
- options
- LoadOption
LoadOption 枚举,用于指定 DataTable 加载选项。
例外
源 IEnumerable<T> 序列为 null
,或者目标 DataTable 为 null
。
注解
输入参数source
的参数T
只能是类型DataRow,也可以是派生自DataRow的类型。
输入序列可以是任何 IEnumerable<T> 源,而不仅仅是对 a DataTable. 请注意,如果源序列是查询,则调用此运算符将强制立即执行。 枚举源序列还可能导致引发异常。
目标表的架构基于源序列中第一 DataRow 行的架构。 表元数据是从 DataRow 元数据中提取的,而表值则从列 DataRow值中提取。 对于类型 DataTable化,不会保留类型。 传输数据和架构,但输出表的结果行不会属于类型化 DataRow 类型。 在RowState从源DataRow复制到返回DataTable的期间,不会保留这些属性和RowError属性。
当在源DataRow中找到值为 null 的 null 引用或可以为 null 的类型时,此方法将目标DataTable中的值替换为值Value。
方法返回的生成的表具有默认表名称。 如果要命名 DataTable、附加到 a DataSet或执行任何其他 DataTable 特定操作,则必须在创建并返回表后执行此操作。
有关详细信息,请参阅 从查询创建 DataTable。
适用于
CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)
在给定其泛型参数 T
为 DataRow 的输入 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> 序列。
- options
- LoadOption
LoadOption 枚举,用于指定 DataTable 加载选项。
- errorHandler
- FillErrorEventHandler
FillErrorEventHandler 委托,表示将处理错误的方法。
例外
源 IEnumerable<T> 序列为 null
,或者目标 DataTable 为 null
。
注解
输入参数source
的参数T
只能是类型DataRow,也可以是派生自DataRow的类型。
输入序列可以是任何 IEnumerable<T> 源,而不仅仅是对 a DataTable. 请注意,如果源序列是查询,则调用此运算符将强制立即执行。 枚举源序列还可能导致引发异常。
目标表的架构基于源序列中第一 DataRow 行的架构。 表元数据是从 DataRow 元数据中提取的,而表值则从列 DataRow值中提取。 对于类型 DataTable化,不会保留类型。 传输数据和架构,但输出表的结果行不会属于类型化 DataRow 类型。 在RowState从源DataRow复制到返回DataTable的期间,不会保留这些属性和RowError属性。
当在源DataRow中找到值为 null 的 null 引用或可以为 null 的类型时,此方法将目标DataTable中的值替换为值Value。
方法返回的生成的表具有默认表名称。 如果要命名 DataTable、附加到 a DataSet或执行任何其他 DataTable 特定操作,则必须在创建并返回表后执行此操作。
如果在将数据行副本复制到目标表中(例如约束异常)期间引发异常, errorHandler
则会调用委托。 将 A FillErrorEventArgs 传递给 errorHandler
具有以下值的委托:
在 Continue 委托调用返回后读取该属性。 Continue如果该属性为true
,则源序列将继续枚举并加载到数据表中。 Continue如果该属性为false
,则从CopyToDataTable方法引发原始异常。
有关详细信息,请参阅 从查询创建 DataTable。