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>)

傳回DataTable,其中包含副本DataRow物件,指定輸入IEnumerable<T>物件其中泛型參數TDataRowReturns 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)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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>)

傳回DataTable,其中包含副本DataRow物件,指定輸入IEnumerable<T>物件其中泛型參數TDataRowReturns 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

來源序列中的物件類型,通常是 DataRowThe type of objects in the source sequence, typically DataRow.

參數

傳回

A 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.

ADataRow在來源序列有狀態的DeletedA DataRow in the source sequence has a state of Deleted.

來源序列未包含任何DataRow物件。The source sequence does not contain any DataRow objects.

ADataRow來源序列的nullA 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,而它會當做 DataGridView 的 Proxy。The 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. RowStateRowError屬性不會保留在從來源複製期間DataRow所傳回DataTableThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

null參考或值為 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.

如需詳細資訊,請參閱 < 建立 DataTable 從查詢For 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)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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

來源序列中的物件類型,通常是 DataRowThe type of objects in the source sequence, typically DataRow.

參數

table
DataTable DataTable DataTable DataTable

目的地 DataTableThe destination DataTable.

options
LoadOption LoadOption LoadOption LoadOption

ALoadOption列舉,指定DataTable載入選項。A LoadOption enumeration that specifies the DataTable load options.

例外狀況

複製DataRow物件不符合的目的地結構描述DataTableThe 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.

ADataRow在來源序列有狀態的DeletedA DataRow in the source sequence has a state of Deleted.

來源序列未包含任何DataRow物件。The source sequence does not contain any DataRow objects.

ADataRow來源序列的nullA 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. RowStateRowError屬性不會保留在從來源複製期間DataRow所傳回DataTableThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

null參考或值為 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.

如需詳細資訊,請參閱 < 建立 DataTable 從查詢For 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)

複本DataRow至指定的物件DataTable,根據輸入IEnumerable<T>物件其中泛型參數TDataRowCopies 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

來源序列中的物件類型,通常是 DataRowThe type of objects in the source sequence, typically DataRow.

參數

table
DataTable DataTable DataTable DataTable

目的地 DataTableThe destination DataTable.

options
LoadOption LoadOption LoadOption LoadOption

ALoadOption列舉,指定DataTable載入選項。A LoadOption enumeration that specifies the DataTable load options.

errorHandler
FillErrorEventHandler FillErrorEventHandler FillErrorEventHandler FillErrorEventHandler

AFillErrorEventHandler委派,表示將處理錯誤的方法。A FillErrorEventHandler delegate that represents the method that will handle an error.

例外狀況

複製DataRow物件不符合的目的地結構描述DataTableThe 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.

ADataRow在來源序列有狀態的DeletedA DataRow in the source sequence has a state of Deleted.

-或--or- 來源序列未包含任何DataRow物件。The source sequence does not contain any DataRow objects.

-或--or- ADataRow來源序列的nullA 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. RowStateRowError屬性不會保留在從來源複製期間DataRow所傳回DataTableThe RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

null參考或值為 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. AFillErrorEventArgs傳遞至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,原始的例外狀況會擲回從CopyToDataTable方法。If the Continue property is false, the original exception is thrown from the CopyToDataTable method.

如需詳細資訊,請參閱 < 建立 DataTable 從查詢For more information, see Creating a DataTable From a Query.

適用於