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 物件 (其中泛型參數 TDataRow) 傳回包含 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)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 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)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 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>)

根據輸入 DataTable 物件 (其中泛型參數 TDataRow) 傳回包含 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

來源序列中的物件類型,通常是 DataRowThe 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 具有 Deleted 的狀態。A 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.

範例

下列範例會查詢 SalesOrderHeader 資料表, 以取得2001年8月8日之後的訂單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或衍生自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 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.

當在來源DataRow中找到null參考或具有null值的 nullable 型別時, 這個方法會將目的地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、將它附加至, 或執行任何其他DataTable特定的DataSet作業, 您必須在建立並傳回資料表之後執行此作業。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)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 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

來源序列中的物件類型,通常是 DataRowThe 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 具有 Deleted 的狀態。A 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.

當在來源DataRow中找到null參考或具有null值的 nullable 型別時, 這個方法會將目的地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、將它附加至, 或執行任何其他DataTable特定的DataSet作業, 您必須在建立並傳回資料表之後執行此作業。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)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 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

來源序列中的物件類型,通常是 DataRowThe 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 具有 Deleted 的狀態。A 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.

當在來源DataRow中找到null參考或具有null值的 nullable 型別時, 這個方法會將目的地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、將它附加至, 或執行任何其他DataTable特定的DataSet作業, 您必須在建立並傳回資料表之後執行此作業。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:

  • Values屬性會設定為來源資料的複本。The Values property is set to a copy of the source data.

  • 屬性會設定為目標DataTableDataTableThe DataTable property is set to the target DataTable.

  • Errors屬性會設定為攔截到的例外狀況。The Errors property is set to the caught exception.

在委派呼叫傳回之後, 會讀取屬性。ContinueThe 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.

適用於