編集

次の方法で共有


DataTableExtensions.CopyToDataTable Method

Definition

Returns a DataTable that contains copies of the DataRow objects, given an input IEnumerable<T> object.

Overloads

CopyToDataTable<T>(IEnumerable<T>)

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

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

Copies DataRow objects to the specified DataTable, given an input IEnumerable<T> object where the generic parameter T is DataRow.

CopyToDataTable<T>(IEnumerable<T>)

Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs

Returns 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

Type Parameters

T

The type of objects in the source sequence, typically DataRow.

Parameters

source
IEnumerable<T>

The source IEnumerable<T> sequence.

Returns

A DataTable that contains the input sequence as the type of DataRow objects.

Exceptions

The source IEnumerable<T> sequence is null and a new table cannot be created.

A DataRow in the source sequence has a state of Deleted.

The source sequence does not contain any DataRow objects.

A DataRow in the source sequence is null.

Examples

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

Remarks

The parameter T of the input parameter source can only be of type DataRow or a type derived from DataRow.

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.

The schema of the destination table is based on the schema of the first DataRow row in the source sequence. The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. For a typed DataTable, types are not preserved. The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

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

For more information, see Creating a DataTable From a Query.

Applies to

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

Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs

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

Type Parameters

T

The type of objects in the source sequence, typically DataRow.

Parameters

source
IEnumerable<T>

The source IEnumerable<T> sequence.

table
DataTable

The destination DataTable.

options
LoadOption

A LoadOption enumeration that specifies the DataTable load options.

Exceptions

The copied DataRow objects do not fit the schema of the destination DataTable.

The source IEnumerable<T> sequence is null or the destination DataTable is null.

A DataRow in the source sequence has a state of Deleted.

The source sequence does not contain any DataRow objects.

A DataRow in the source sequence is null.

Remarks

The parameter T of the input parameter source can only be of type DataRow, or a type derived from DataRow.

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.

The schema of the destination table is based on the schema of the first DataRow row in the source sequence. The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. For a typed DataTable, types are not preserved. The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

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

For more information, see Creating a DataTable From a Query.

Applies to

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

Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs
Source:
DataTableExtensions.cs

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

Type Parameters

T

The type of objects in the source sequence, typically DataRow.

Parameters

source
IEnumerable<T>

The source IEnumerable<T> sequence.

table
DataTable

The destination DataTable.

options
LoadOption

A LoadOption enumeration that specifies the DataTable load options.

errorHandler
FillErrorEventHandler

A FillErrorEventHandler delegate that represents the method that will handle an error.

Exceptions

The copied DataRow objects do not fit the schema of the destination DataTable.

The source IEnumerable<T> sequence is null or the destination DataTable is null.

A DataRow in the source sequence has a state of Deleted.

-or-

The source sequence does not contain any DataRow objects.

-or-

A DataRow in the source sequence is null.

Remarks

The parameter T of the input parameter source can only be of type DataRow, or a type derived from DataRow.

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.

The schema of the destination table is based on the schema of the first DataRow row in the source sequence. The table metadata is extracted from the DataRow metadata and the table values from the column values of the DataRow. For a typed DataTable, types are not preserved. The data and schema are transferred, but the resulting rows of the output table will not be of the typed DataRow type. The RowState and RowError properties are not preserved during the copy from the source DataRow to the returned DataTable.

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

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. A FillErrorEventArgs is passed to the errorHandler delegate with the following values:

  • The Values property is set to a copy of the source data.

  • The DataTable property is set to the target DataTable.

  • The Errors property is set to the caught exception.

The Continue property is read after the delegate call returns. If the Continue property is true, the source sequence continues to be enumerated and loaded into the data table. If the Continue property is false, the original exception is thrown from the CopyToDataTable method.

For more information, see Creating a DataTable From a Query.

Applies to