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

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 方法 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. RowState RowError 從來源複製到傳回的期間,不會保留和屬性 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.

如需詳細資訊,請參閱 從查詢建立 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 ,或是衍生自的型別 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. RowState RowError 從來源複製到傳回的期間,不會保留和屬性 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.

如需詳細資訊,請參閱 從查詢建立 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;
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 ,或是衍生自的型別 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. RowState RowError 從來源複製到傳回的期間,不會保留和屬性 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. FillErrorEventArgs會傳遞至 errorHandler 具有下列值的委派:A FillErrorEventArgs is passed to the errorHandler delegate with the following values:

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

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

  • Errors屬性設定為攔截到的例外狀況。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 ,則會從方法擲回原始例外狀況 CopyToDataTableIf the Continue property is false, the original exception is thrown from the CopyToDataTable method.

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

適用於