DataTableExtensions.CopyToDataTable 方法

定義

根據輸入 DataTable 物件,傳回包含 DataRow 物件複本的 IEnumerable<T>

多載

CopyToDataTable<T>(IEnumerable<T>)

根據輸入 DataTable 物件 (其中泛型參數 TDataRow) 傳回包含 IEnumerable<T> 物件複本的 DataRow

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 IEnumerable<T> 物件複製到指定的 DataRow

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

根據輸入 DataRow 物件 (其中泛型參數 TDataTable),將 IEnumerable<T> 物件複製到指定的 DataRow

CopyToDataTable<T>(IEnumerable<T>)

根據輸入 DataTable 物件 (其中泛型參數 TDataRow) 傳回包含 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,而且無法建立新的資料表。

來源序列中的 DataRow 具有 Deleted 的狀態。

來源序列不含任何 DataRow 物件。

來源序列中的 DataRownull

範例

下列範例會在 2001 年 8 月 8 日之後查詢 SalesOrderHeader 資料表,並使用 CopyToDataTable 方法來從該查詢建立 DataTable 。 接著,DataTable 便繫結至 BindingSource,而它會當做 DataGridView 的 Proxy。

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

備註

輸入參數 Tsource 的參數只能是 型 DataRow 別或衍生自 DataRow 的類型。

輸入序列可以是任何 IEnumerable<T> 來源,而不只是查詢或透過 的 DataTable 查詢。 請注意,如果來源序列是查詢,則呼叫這個運算子將會強制立即執行。 列舉來源序列也會導致擲回例外狀況。

目的地資料表的架構是以來源序列中第一個 DataRow 資料列的架構為基礎。 資料表中繼資料會從 DataRow 中繼資料擷取,並從 的資料行值擷取資料表值 DataRow 。 針對具 DataTable 類型的 ,不會保留型別。 資料與架構會傳輸,但輸出資料表產生的資料列將不會屬於具型別 DataRow 的類型。 和 RowStateRowError 屬性不會在從來源 DataRow 複製到傳 DataTable 回的 期間保留。

在來源 DataRow 中找到null參考或可為 Null的型別時,這個方法會將目的地 DataTable 中的值取代為 值 Value

方法傳回的產生資料表具有預設資料表名稱。 如果您想要將 命名 DataTable 為 ,請將它附加至 DataSet ,或執行任何其他 DataTable 特定作業,則必須在建立並傳回資料表之後執行此動作。

如需詳細資訊,請參閱 從查詢建立 DataTable

適用於

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption)

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

table
DataTable

目的 DataTable

options
LoadOption

指定 LoadOption 載入選項的 DataTable 列舉型別。

例外狀況

已複製的 DataRow 物件不適合目的 DataTable 的結構描述。

來源 IEnumerable<T> 序列為 null,或者目的 DataTablenull

來源序列中的 DataRow 具有 Deleted 的狀態。

來源序列不含任何 DataRow 物件。

來源序列中的 DataRownull

備註

輸入參數 Tsource 的參數只能是 型 DataRow 別 ,或衍生自 DataRow 的類型。

輸入序列可以是任何 IEnumerable<T> 來源,而不只是透過 的 DataTable 查詢。 請注意,如果來源序列是查詢,則呼叫這個運算子將會強制立即執行。 列舉來源序列也會導致擲回例外狀況。

目的地資料表的架構是以來源序列中第一個 DataRow 資料列的架構為基礎。 資料表中繼資料會從 DataRow 中繼資料擷取,並從 的資料行值擷取資料表值 DataRow 。 針對具 DataTable 類型的 ,不會保留型別。 資料與架構會傳輸,但輸出資料表產生的資料列將不會屬於具型別 DataRow 的類型。 和 RowStateRowError 屬性不會在從來源 DataRow 複製到傳 DataTable 回的 期間保留。

在來源 DataRow 中找到null參考或可為 Null的型別時,這個方法會將目的地 DataTable 中的值取代為 值 Value

方法傳回的產生資料表具有預設資料表名稱。 如果您想要將 命名 DataTable 為 ,請將它附加至 DataSet ,或執行任何其他 DataTable 特定作業,則必須在建立並傳回資料表之後執行此動作。

如需詳細資訊,請參閱 從查詢建立 DataTable

適用於

CopyToDataTable<T>(IEnumerable<T>, DataTable, LoadOption, FillErrorEventHandler)

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

table
DataTable

目的 DataTable

options
LoadOption

指定 LoadOption 載入選項的 DataTable 列舉型別。

errorHandler
FillErrorEventHandler

FillErrorEventHandler 委派,表示會處理錯誤的方法。

例外狀況

已複製的 DataRow 物件不適合目的 DataTable 的結構描述。

來源 IEnumerable<T> 序列為 null,或者目的 DataTablenull

來源序列中的 DataRow 具有 Deleted 的狀態。

-或-

來源序列不含任何 DataRow 物件。

-或-

來源序列中的 DataRownull

備註

輸入參數 Tsource 的參數只能是 型 DataRow 別 ,或衍生自 DataRow 的類型。

輸入序列可以是任何 IEnumerable<T> 來源,而不只是透過 的 DataTable 查詢。 請注意,如果來源序列是查詢,則呼叫這個運算子將會強制立即執行。 列舉來源序列也會導致擲回例外狀況。

目的地資料表的架構是以來源序列中第一個 DataRow 資料列的架構為基礎。 資料表中繼資料會從 DataRow 中繼資料擷取,並從 的資料行值擷取資料表值 DataRow 。 針對具 DataTable 類型的 ,不會保留型別。 資料與架構會傳輸,但輸出資料表產生的資料列將不會屬於具型別 DataRow 的類型。 和 RowStateRowError 屬性不會在從來源 DataRow 複製到傳 DataTable 回的 期間保留。

在來源 DataRow 中找到null參考或可為 Null的型別時,這個方法會將目的地 DataTable 中的值取代為 值 Value

方法傳回的產生資料表具有預設資料表名稱。 如果您想要將 命名 DataTable 為 ,請將它附加至 DataSet ,或執行任何其他 DataTable 特定作業,則必須在建立並傳回資料表之後執行此動作。

如果在將資料列複本複製到目標資料表期間擲回例外狀況,例如條件約束例外狀況,則會 errorHandler 呼叫委派。 FillErrorEventArgs會傳遞至 errorHandler 具有下列值的委派:

  • 屬性 Values 會設定為來源資料的複本。

  • 屬性 DataTable 會設定為目標 DataTable

  • 屬性 Errors 會設定為攔截的例外狀況。

屬性 Continue 會在委派呼叫傳回之後讀取。 Continue如果 屬性為 true ,來源序列會繼續列舉並載入至資料表。 Continue如果 屬性為 false ,則會從 方法擲回原始例外狀況 CopyToDataTable

如需詳細資訊,請參閱 從查詢建立 DataTable

適用於