Export (0) Print
Expand All

DbDataAdapter::Fill Method (DataTable, IDbCommand, CommandBehavior)

Adds or refreshes rows in a DataTable to match those in the data source using the specified DataTable, IDbCommand and CommandBehavior.

Namespace:  System.Data.Common
Assembly:  System.Data (in System.Data.dll)

protected:
virtual int Fill(
	DataTable^ dataTable, 
	IDbCommand^ command, 
	CommandBehavior behavior
)

Parameters

dataTable
Type: System.Data::DataTable
A DataTable to fill with records and, if necessary, schema.
command
Type: System.Data::IDbCommand
The SQL SELECT statement used to retrieve rows from the data source.
behavior
Type: System.Data::CommandBehavior
One of the CommandBehavior values.

Return Value

Type: System::Int32
The number of rows successfully added to or refreshed in the DataTable. This does not include rows affected by statements that do not return rows.

The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data and then closed. If the connection is open before Fill is called, it remains open.

The Fill operation then adds the rows to the specified destination DataTable object in the DataSet, creating the DataTable object if it does not already exist. When creating a DataTable object, the Fill operation normally creates only column name metadata. However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

If the DbDataAdapter object encounters duplicate columns while populating a DataTable, it will generate names for the subsequent columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. If the incoming data contains unnamed columns, they are placed in the DataSet according to the pattern "Column1", "Column2", and so on.

The overload of Fill that takes DataTable as a parameter only obtains the first result. Use an overload of Fill that takes DataSet as a parameter to obtain multiple results.

You can use the Fill method multiple times on the same DataTable. If a primary key exists, incoming rows are merged with matching rows that already exist. If no primary key exists, incoming rows are appended to the DataTable.

If the SelectCommand returns the results of an OUTER JOIN, the DataAdapter does not set a PrimaryKey value for the resulting DataTable. You must explicitly define the primary key to ensure that duplicate rows are resolved correctly. For more information, see Defining Primary Keys (ADO.NET).

NoteNote

When handling batch SQL statements that return multiple results, the implementation of FillSchema for the .NET Framework Data Provider for OLE DB retrieves schema information for only the first result. To retrieve schema information for multiple results, use Fill with the MissingSchemaAction set to AddWithKey.

Notes to Implementers

This overload of the Fill method is protected and is designed for use by a .NET Framework data provider.

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft