myPrimaryKey = OrderID
myDataColumn.ColumnName = OrderID
myDataColumn.DataType = System.Int32
myDataColumn.AllowDBNull = False
myDataColumn.AutoIncrement = False
myDataColumn.AutoIncrementSeed = 0
myDataColumn.AutoIncrementStep = 1
myDataColumn.MaxLength = -1
myDataColumn.ReadOnly = False
myDataColumn.Unique = True Reading from the Order Details DataTable:
myPrimaryKey = OrderID
myPrimaryKey = ProductID
myDataColumn.ColumnName = OrderID
myDataColumn.DataType = System.Int32
myDataColumn.AllowDBNull = False
myDataColumn.AutoIncrement = False
myDataColumn.AutoIncrementSeed = 0
myDataColumn.AutoIncrementStep = 1
myDataColumn.MaxLength = -1
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
myDataColumn.ColumnName = ProductID
myDataColumn.DataType = System.Int32
myDataColumn.AllowDBNull = False
myDataColumn.AutoIncrement = False
•
The DataColumn .NET data type-which is stored in the DataType property.
•
The maximum length of a variable length data type-which is stored in the
MaxLength property.
•
Whether the DataColumn can accept a null value-which is stored in the
AllowDBNull property.
•
Whether the DataColumn value must be unique-which is stored in the Unique
property.
•
Any auto-increment information-which is stored in the AutoIncrement,
AutoIncrementSeed, and AutoIncrementStep properties.
The FillSchema() method will also determine whether the DataColumn is part of a
primary key and store that information in the PrimaryKey property of the DataTable.
Warning
FillSchema() does not automatically add ForeignKeyConstraint objects to the
DataTable objects. Neither does it retrieve the actual rows from the database; it
retrieves only the schema information.
The FillSchema() method is overloaded, with the most commonly used version of this
method being the following:
DataTable[] FillSchema(DataSet myDataSet, SchemaType mySchemaType)
where mySchemaType specifies how you want to handle any existing schema mappings.
You set mySchemaType to one of the constants defined in the System.Data.SchemaType
enumeration. Table 11.7
shows the constants defined in the SchemaType enumeration.
Table 11.7: SchemaType ENUMERATION MEMBERS
CONSTANT DESCRIPTION
Mapped Apply any existing table mappings to the incoming schema and configure
/*
FillSchema.cs illustrates how to read schema information
using the FillSchema() method of a DataAdapter object
*/
using System;
using System.Data;
using System.Data.SqlClient;
class FillSchema
{
public static void Main()
{
SqlConnection mySqlConnection =
new SqlConnection(
"server=localhost;database=Northwind;uid=sa;pwd=sa"
);
SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
mySqlCommand.CommandText =
"SELECT ProductID, ProductName " +
"FROM Products;" +
"SELECT OrderID " +
"FROM Orders;" +
"SELECT OrderID, ProductID, UnitPrice " +
"FROM [Order Details];";
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
mySqlDataAdapter.SelectCommand = mySqlCommand;
DataSet myDataSet = new DataSet();
mySqlConnection.Open();
}
// display some of the details for each column
foreach (DataColumn myDataColumn in myDataTable.Columns)
{
Console.WriteLine("\nmyDataColumn.ColumnName = " +
myDataColumn.ColumnName);
Console.WriteLine("myDataColumn.DataType = " +
myDataColumn.DataType);
Console.WriteLine("myDataColumn.AllowDBNull = " +
myDataColumn.AllowDBNull);
Console.WriteLine("myDataColumn.AutoIncrement = " +
myDataColumn.AutoIncrement);
Console.WriteLine("myDataColumn.AutoIncrementSeed = " +
myDataColumn.AutoIncrementSeed);
Console.WriteLine("myDataColumn.AutoIncrementStep = " +
myDataColumn.AutoIncrementStep);
Console.WriteLine("myDataColumn.MaxLength = " +
myDataColumn.MaxLength);
Console.WriteLine("myDataColumn.ReadOnly = " +
myDataColumn.ReadOnly);
Console.WriteLine("myDataColumn.Unique = " +
myDataColumn.Unique);
}
}
}
}
The output from this program is as follows: