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

來源:
DataTableExtensions.cs
來源:
DataTableExtensions.cs
來源:
DataTableExtensions.cs

根據輸入 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 。 在RowState從來源DataRow複製到傳DataTable回 的 期間,不會保留 和 RowError 屬性。

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

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

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

適用於

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

來源:
DataTableExtensions.cs
來源:
DataTableExtensions.cs
來源:
DataTableExtensions.cs

根據輸入 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 。 在RowState從來源DataRow複製到傳DataTable回 的 期間,不會保留 和 RowError 屬性。

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

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

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

適用於

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

來源:
DataTableExtensions.cs
來源:
DataTableExtensions.cs
來源:
DataTableExtensions.cs

根據輸入 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 。 在RowState從來源DataRow複製到傳DataTable回 的 期間,不會保留 和 RowError 屬性。

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

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

如果在目標數據表中的數據列復本期間擲回例外狀況,例如條件約束例外狀況, errorHandler 則會呼叫委派。 會 FillErrorEventArgs 以下列值傳遞至 errorHandler 委派:

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

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

適用於