IDataAdapter.Fill(DataSet) Method

Definition

Adds or updates rows in the DataSet to match those in the data source using the DataSet name, and creates a DataTable named "Table".

public:
 int Fill(System::Data::DataSet ^ dataSet);
public int Fill (System.Data.DataSet dataSet);
abstract member Fill : System.Data.DataSet -> int
Public Function Fill (dataSet As DataSet) As Integer

Parameters

dataSet
DataSet

A DataSet to fill with records and, if necessary, schema.

Returns

The number of rows successfully added to or refreshed in the DataSet. This does not include rows affected by statements that do not return rows.

Remarks

Fill 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, then closed. If the connection is open before Fill is called, it remains open.

The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. When creating DataTable objects, the Fill operation usually creates only column name metadata. However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

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.

If the IDataAdapter encounters duplicate columns while populating a DataTable, it generates names for the successive columns, using the pattern "columnname1", "columnname2", "columnname3", and so on. If the incoming data contains unnamed columns, they are put in the DataSet according to the pattern "Column1", "Column2", and so on. When multiple result sets are added to the DataSet, each result set is put in a separate table. Additional result sets are named by appending integral values to the specified table name (for example, "Table", "Table1", "Table2", and so on). Applications should use caution when using column and table names to ensure that conflicts with these naming patterns do not occur.

When the SELECT statement that is used to populate the DataSet returns multiple results, such as a batch SQL statement, if one of the results contains an error, all successive results are skipped and not added to the DataSet.

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.

Note

When handling batch SQL statements that return multiple results, the implementation of Fill and FillSchema for a .NET Framework data provider retrieves schema information for only the first result.

Applies to

See also