DbDataAdapter.FillSchema Method (DataSet, SchemaType, String)
Assembly: System.Data (in System.Data.dll)
'Declaration Public Function FillSchema ( _ dataSet As DataSet, _ schemaType As SchemaType, _ srcTable As String _ ) As DataTable()
- Type: System.Data.SchemaType
One of the SchemaType values that specify how to insert the schema.
- Type: System.String
The name of the source table to use for table mapping.
A source table from which to get the schema could not be found.
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:
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.
If a unique clustered index is defined on a column or columns in a SQL Server table and the primary key constraint is defined on a separate set of columns, then the names of the columns in the clustered index will be returned. To return the name or names of the primary key columns, use a query hint with the SELECT statement that specifies the name of the primary key index. For more information about specifying query hints, see Query Hint (Transact-SQL).
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 using column and table names should ensure that conflicts with these naming patterns does not occur.
The FillSchema method supports scenarios where the DataSet contains multiple DataTable objects whose names differ only by case. In such situations, FillSchema 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.FillSchema(dataset, "aaa"); // Fills the schema of "aaa", which already exists in the DataSet. adapter.FillSchema(dataset, "Aaa"); // Adds a new table called "Aaa".
If FillSchema 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.FillSchema(dataset, "AAA"); // Fills the schema of table "aaa" because only one similarly named table is in the DataSet.
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.
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.
Private Function GetCustomerData(ByVal dataSetName As String, _ ByVal connectionString As String) As DataSet Dim dataSet As DataSet = New DataSet(dataSetName) Using connection As SqlConnection = New SqlConnection(connectionString) Dim adapter As SqlDataAdapter = New SqlDataAdapter( _ "SELECT CustomerID, CompanyName, ContactName FROM dbo.Customers", _ connection) Dim mapping As DataTableMapping = adapter.TableMappings.Add( _ "Table", "Customers") mapping.ColumnMappings.Add("CompanyName", "Name") mapping.ColumnMappings.Add("ContactName", "Contact") connection.Open() adapter.FillSchema(dataSet, SchemaType.Source, "Customers") adapter.Fill(dataSet) Return dataSet End Using End Function
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.