Export (0) Print
Expand All

DbDataAdapter.FillSchema Method (DataTable, SchemaType)

.NET Framework 1.1

Configures the schema of the specified DataTable based on the specified SchemaType.

[Visual Basic]
Overloads Public Function FillSchema( _
   ByVal dataTable As DataTable, _
   ByVal schemaType As SchemaType _
) As DataTable
[C#]
public DataTable FillSchema(
 DataTable dataTable,
 SchemaType schemaType
);
[C++]
public: DataTable* FillSchema(
 DataTable* dataTable,
 SchemaType schemaType
);
[JScript]
public function FillSchema(
   dataTable : DataTable,
 schemaType : SchemaType
) : DataTable;

Parameters

dataTable
The DataTable to be filled with the schema from the data source.
schemaType
One of the SchemaType values.

Return Value

A DataTable that contains schema information returned from the data source.

Remarks

The FillSchema method retrieves the schema from the data source using the SelectCommand. The connection object associated with the SelectCommand must be valid, but it does not need to be open. If the connection is closed before FillSchema is called, it is opened to retrieve data, then closed. If the connection is open before FillSchema is called, it remains open.

A FillSchema operation returns a DataTable. 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 a PrimaryKey has already been defined for the DataTable, or the DataTable contains data, the PrimaryKey property will not be set.
  • 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. This process may require several round-trips to the server.

If the DbDataAdapter 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.

FillSchema does not return any rows. Use the Fill method to add rows to a DataTable.

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.

Example

[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 a SqlDataAdapter and a DataSet.

[Visual Basic] 
Public Shared Function GetCustomerData(dataTableName As String) As DataTable

  Dim dt As DataTable = New DataTable(dataTableName)

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

  conn.Open()

  da.FillSchema(dt, SchemaType.Mapped)

  da.Fill(dt)

  conn.Close()

  Return dt
End Function

[C#] 
public static DataTable GetCustomerData(string dataTableName)
{
  DataTable dt = new DataTable(dataTableName);

  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");

  conn.Open();

  da.FillSchema(dt, SchemaType.Mapped);

  da.Fill(dt);

  conn.Close();

  return dt;
}

[C++] 
public:
    static DataTable* GetCustomerData(String* dataTableName)
    {
        DataTable* dt = new DataTable(dataTableName);

        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");

        conn->Open();

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

        da->Fill(dt);

        conn->Close();

        return dt;
    };    // End of GetCustomerData()

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

Requirements

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

Show:
© 2014 Microsoft