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

根據輸入 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)

根據輸入 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)

根據輸入 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 物件 (其中泛型參數 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.

參數

source
IEnumerable<T>

來源 IEnumerable<T> 序列。The source IEnumerable<T> sequence.

傳回

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.

範例

下列範例會在2001年8月8日之後查詢 SalesOrderHeader 資料表中的訂單,並使用 CopyToDataTable 方法,從該查詢建立 DataTableThe 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.

輸入序列可以是任何 @no__t 0 來源,而不只是 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.

目的地資料表的架構是以來源序列中第一個 @no__t 0 資料列的架構為基礎。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. 從來源 DataRow 複製到傳回的 DataTable 時,不會保留 RowState 和 @no__t 1 屬性。The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在來源 DataRow 中找到null參考或具有null值的 nullable 型別時,這個方法會將目的地 DataTable 中的值取代為 Value 的值。When 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. 如果您想要將 @no__t 的名稱設為0,請將它附加至 DataSet,或執行任何其他 @no__t 2 特定的作業,您必須在建立並傳回資料表之後執行此作業。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)

根據輸入 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.

參數

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 具有 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.

輸入序列可以是任何 @no__t 0 來源,而不只是透過 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.

目的地資料表的架構是以來源序列中第一個 @no__t 0 資料列的架構為基礎。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. 從來源 DataRow 複製到傳回的 DataTable 時,不會保留 RowState 和 @no__t 1 屬性。The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在來源 DataRow 中找到null參考或具有null值的 nullable 型別時,這個方法會將目的地 DataTable 中的值取代為 Value 的值。When 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. 如果您想要將 @no__t 的名稱設為0,請將它附加至 DataSet,或執行任何其他 @no__t 2 特定的作業,您必須在建立並傳回資料表之後執行此作業。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)

根據輸入 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.

參數

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 具有 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.

輸入序列可以是任何 @no__t 0 來源,而不只是透過 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.

目的地資料表的架構是以來源序列中第一個 @no__t 0 資料列的架構為基礎。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. 從來源 DataRow 複製到傳回的 DataTable 時,不會保留 RowState 和 @no__t 1 屬性。The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

在來源 DataRow 中找到null參考或具有null值的 nullable 型別時,這個方法會將目的地 DataTable 中的值取代為 Value 的值。When 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. 如果您想要將 @no__t 的名稱設為0,請將它附加至 DataSet,或執行任何其他 @no__t 2 特定的作業,您必須在建立並傳回資料表之後執行此作業。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. @No__t-0 會傳遞至具有下列值的 @no__t 1 委派:A FillErrorEventArgs is passed to the errorHandler delegate with the following values:

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

  • @No__t-0 屬性設為目標 DataTableThe DataTable property is set to the target DataTable.

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

在委派呼叫傳回之後,會讀取 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,則會從 @no__t 2 方法擲回原始例外狀況。If the Continue property is false, the original exception is thrown from the CopyToDataTable method.

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

適用於