FillSchema Method (DataSet, SchemaType)

DbDataAdapter.FillSchema Method (DataSet, SchemaType)

.NET Framework 1.1

Adds a DataTable named "Table" to the specified DataSet and configures the schema to match that in the data source based on the specified SchemaType.

[Visual Basic]
Overrides Overloads Public Function FillSchema( _
   ByVal dataSet As DataSet, _
   ByVal schemaType As SchemaType _
) As DataTable() Implements IDataAdapter.FillSchema
public override DataTable[] FillSchema(
 DataSet dataSet,
 SchemaType schemaType
public: DataTable* FillSchema(
 DataSet* dataSet,
 SchemaType schemaType
) [];
public override function FillSchema(
   dataSet : DataSet,
 schemaType : SchemaType
) : DataTable[];


A DataSet to insert the schema in.
One of the SchemaType values that specify how to insert the schema.

Return Value

A reference to a collection of DataTable objects that were added to the DataSet.




This method retrieves the schema information from the data source using the SelectCommand.

A FillSchema operation adds a DataTable to the destination DataSet. It then adds columns to the DataColumnCollection of the DataTable, and configures the following DataColumn properties if they exist at the data source:

FillSchema also configures the PrimaryKey and Constraints properties according to the following rules:

  • If one or more primary key columns are returned by the SelectCommand, they are used as the primary key columns for the DataTable.
  • If no primary key columns are returned but unique columns are, the unique columns are used as the primary key if, and only if, all the unique columns are nonnullable. If any of the columns are nullable, a UniqueConstraint is added to the ConstraintCollection, but the PrimaryKey property is not set.
  • If both primary key columns and unique columns are returned, the primary key columns are used as the primary key columns for the DataTable.

Note that primary keys and unique constraints are added to the ConstraintCollection according to the preceding rules, but other constraint types are not added.

Primary key information is used during Fill to find and replace any rows whose key columns match. If this is not the desired behavior, use Fill without requesting schema information.

If the IDataAdapter encounters duplicate columns while populating a DataTable, it generates 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 multiple result sets are added to the DataSet 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.). Applications should use caution when using column and table names to ensure that conflicts with these naming patterns does not occur.

The IDbConnection object associated with the select command must be valid, but it does not need to open. If the IDbConnection is closed before FillSchema is called, it is opened to retrieve data, then closed. If the connection is open before FillSchema is called, it is left open.

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

When using FillSchema, the .NET Framework Data Provider for SQL Server appends a FOR BROWSE clause to the statement being executed. The user should be aware of potential side effects, such as interference with the use of SET FMTONLY ON statements. See SQL Server Books Online for more information.


[Visual Basic, C#, C++] The following example uses the derived class, SqlDataAdapter, to fill a DataSet with the schema, an array of DataTable objects with data, and returns the SqlParameterCollection collection to an array of IDataParameter objects. This example assumes that you have created an SqlDataAdapter and a DataSet.

[Visual Basic] 
Public Shared Function GetCustomerData(dataSetName As String) As DataSet

  Dim ds As DataSet = New DataSet(dataSetName)

  Dim conn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind")

  Dim da As SqlDataAdapter = New SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName FROM Customers", conn) 

  Dim tm As DataTableMapping = da.TableMappings.Add("Table", "Customers")
  tm.ColumnMappings.Add("CompanyName", "Name")
  tm.ColumnMappings.Add("ContactName", "Contact")


  da.FillSchema(ds, SchemaType.Mapped)



  Return ds
End Function

public static DataSet GetCustomerData(string dataSetName)
  DataSet ds = new DataSet(dataSetName);

  SqlConnection conn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind");

  SqlDataAdapter da = new SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName FROM Customers", conn); 

  DataTableMapping tm = da.TableMappings.Add("Table", "Customers");
  tm.ColumnMappings.Add("CompanyName", "Name");
  tm.ColumnMappings.Add("ContactName", "Contact");


  da.FillSchema(ds, SchemaType.Mapped);



  return ds;

  static DataSet* GetCustomerData(String* dataSetName)
    DataSet* ds = new DataSet(dataSetName);

    SqlConnection* conn = new SqlConnection(S"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind");

    SqlDataAdapter* da = new SqlDataAdapter(S"SELECT CustomerID, CompanyName, ContactName FROM Customers", conn); 

    DataTableMapping* tm = da->TableMappings->Add(S"Table", S"Customers");
    tm->ColumnMappings->Add(S"CompanyName", S"Name");
    tm->ColumnMappings->Add(S"ContactName", S"Contact");


    da->FillSchema(ds, SchemaType::Mapped);



    return ds;

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

DbDataAdapter Class | DbDataAdapter Members | System.Data.Common Namespace | DbDataAdapter.FillSchema Overload List

© 2015 Microsoft