Export (0) Print
Expand All

DbDataAdapter.Fill Method (DataSet, String)

Adds or refreshes rows in the DataSet to match those in the data source using the DataSet and DataTable names.

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

public int Fill(
	DataSet dataSet,
	string srcTable
)

Parameters

dataSet
Type: System.Data.DataSet

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

srcTable
Type: System.String

The name of the source table to use for table mapping.

Return Value

Type: System.Int32
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.

ExceptionCondition
SystemException

The source table is invalid.

The Fill method retrieves the data from the data source using a SELECT statement. The IDbConnection object associated with the select command must be valid, but it does not need to be open. If the IDbConnection 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.

If a command does not return any rows, no tables are added to the DataSet, and no exception is raised.

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.

When the query specified returns multiple results, each result set is placed 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). Since no table is created for a query that does not return rows, if you were to process an insert query followed by a select query, the table created for the select query would be named "Table", because it is the first table created. Applications using column and table names should ensure that conflicts with these naming patterns does not occur.

The Fill method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. In such situations, Fill performs a case-sensitive comparison to find the corresponding table, and creates a new table if no exact match exists. The following C# code illustrates this behavior.

 DataSet dataset = new DataSet();
 dataset.Tables.Add("aaa");
 dataset.Tables.Add("AAA");
 adapter.Fill(dataset, "aaa"); // Fills "aaa", which already exists in the DataSet.
 adapter.Fill(dataset, "Aaa"); // Adds a new table called "Aaa".

If Fill is called and the DataSet contains only one DataTable whose name differs only by case, that DataTable is updated. In this scenario, the comparison is case insensitive. The following C# code illustrates this behavior.

 DataSet dataset = new DataSet();
 dataset.Tables.Add("aaa");
 adapter.Fill(dataset, "AAA"); // Fills table "aaa" because only one similarly named table is in the DataSet.

If an error or an exception is encountered while populating the data tables, rows added prior to the occurrence of the error remain in the data tables. The remainder of the operation is aborted.

When the SELECT statement used to populate the DataSet returns multiple results, such as a batch SQL statement, be aware of the following:

  • If one of the results contains an error, all subsequent results are skipped and not added to the DataSet.

When using subsequent Fill calls to refresh the contents of the DataSet, two conditions must be met:

  1. The SQL statement should match the one initially used to populate the DataSet.

  2. The Key column information must be present. If primary key information is present, any duplicate rows are reconciled and only appear once in the DataTable that corresponds to the DataSet. Primary key information may be set either through FillSchema, by specifying the PrimaryKey property of the DataTable, or by setting the MissingSchemaAction property to AddWithKey.

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, seeDefining Primary Keys.

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 Inheritors

When overriding Fill in a derived class, be sure to call the base class's Fill method.

The following example uses the derived class, OleDbDataAdapter, to fill a DataSet with rows from the categories table. This example assumes that you have created an OleDbDataAdapter and a DataSet.

public void GetRecords() 
{
    // ... 
    // create dataSet and adapter 
    // ...
    adapter.Fill(dataSet, "Categories");
}

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft