DataAdapterFillSchema

This application illustrates the operations of the DataAdapter's FillSchema() method.  That method loads a DataSet object with schema information from a database specified in the DataAdapter's Connection property, and from tables referenced in its SelectCommand property.

The CommandText for the DataAdapter's SelectCommand is set at runtime and can be controlled by you, the user.  You can type in your own command, or select from pre-constructed CommandText strings using the "Select Query" ComboBox.  The three pre-constructed queries are as follows:

1.	Single Table
2.	Parent-Child
3.	Complex Relationship

The Single Table query select columns from a single Northwind table, Products:

SELECT ProductID, ProductName, SupplierID, CategoryID, QuantityPerUnit, UnitPrice, UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued FROM Products

The Parent-Child query selects columns from an inner join of the Categories and Products tables:

SELECT Categories.CategoryID, Categories.CategoryName, Categories.Description, Products.ProductID, Products.ProductName, Products.UnitPrice FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID

The Complex Relationship query selects columns from a many-way join of the Categories, Products, Suppliers, Order Details, and Orders  tables:

SELECT Orders.OrderID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, [Order Details].OrderID AS Expr1, [Order Details].ProductID, [Order Details].UnitPrice, [Order Details].Quantity, [Order Details].Discount, Products.ProductID AS Expr2, Products.ProductName, Products.SupplierID, Suppliers.SupplierID AS Expr3, Suppliers.CompanyName, Suppliers.ContactName, Suppliers.ContactTitle, Categories.CategoryID, Categories.CategoryName, Categories.Description FROM Categories INNER JOIN Products ON Categories.CategoryID = Products.CategoryID INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID INNER JOIN [Order Details] ON Products.ProductID = [Order Details].ProductID INNER JOIN Orders ON [Order Details].OrderID = Orders.OrderID

Once you have specified the CommandText for the SELECT command, you can cause the FillSchema() method to be invoked by clicking the <FillSchema> button.  The method will be invoked with schematype parameter value of either SchemaType.Mapped or SchemaType.Source, according to your selection on the form.

            If Me.radSchemaType_Mapped.Checked Then
                daQuery.FillSchema(m_dsData, SchemaType.Mapped)
            ElseIf Me.radSchemaType_Source.Checked Then
                daQuery.FillSchema(m_dsData, SchemaType.Source)
            End If

The first mode setting incorporates any table mappings that have been applied to the DataAdapter when loading the schema; the second mode setting ignores table mappings.  So you can see the difference, I've applied a simple mapping of the name of the incoming "table" using the following code.

            Select Case Me.cboQuery.SelectedIndex
                Case QueryType.SingleTable
                    daQuery.TableMappings.Add("Table", "SimpleTable")
                Case QueryType.ParentChild
                    daQuery.TableMappings.Add("Table", "ParentChild")
                Case QueryType.ComplexRelationship
                    daQuery.TableMappings.Add("Table", "ComplexRelationship")
            End Select

Note that ADO.NET treats the name of the sourcetable (first) parameter as "Table" when the CommandType is CommandText; so it is that name that we must map to something else.  I've mapped the table name to strings that reflect your query selection from the ComboBox.

If you select the Simple Table query, and cause FillSchema() to be called with the SchemaType.Mapped schematype setting, the first few lines of the generated output will appear as follows: 

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="SimpleTable">
          
If, on the other hand, you specify that FillSchema() should be called with the SchemaType.Source schematype setting, the same lines will appear like this:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true">
    <xs:complexType>
      <xs:choice maxOccurs="unbounded">
        <xs:element name="Table">
        
Only the last-listed line differs between the two cases.