DataTableReader.GetSchemaTable Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Returns a DataTable that describes the column metadata of the DataTableReader.
public:
override System::Data::DataTable ^ GetSchemaTable();
public override System.Data.DataTable GetSchemaTable ();
override this.GetSchemaTable : unit -> System.Data.DataTable
Public Overrides Function GetSchemaTable () As DataTable
Returns
A DataTable that describes the column metadata.
Exceptions
The DataTableReader is closed.
Examples
The following console application example retrieves schema information about the specified column. Pass the DisplaySchemaTableInfo
procedure a DataTableReader and an integer representing the ordinal position of a column within the DataTableReader
, and the procedure outputs schema information to the console window.
private static void TestGetSchemaTable()
{
// Set up the data adapter, using information from
// the AdventureWorks sample database.
// Modify the SQL expression to retrieve
// data from a different table.
SqlDataAdapter adapter =
SetupDataAdapter("SELECT * FROM Sales.Customer");
// Fill the DataTable, retrieving all the schema information.
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
DataTable table = new DataTable();
adapter.Fill(table);
// Create the DataTableReader, and close it when done.
using (DataTableReader reader = new DataTableReader(table))
{
// Modify the column number to display information
// about a column other than column 0.
DisplaySchemaTableInfo(reader, 0);
}
Console.WriteLine();
Console.WriteLine("Press Enter to finish.");
Console.ReadLine();
}
private static void DisplaySchemaTableInfo(
DataTableReader reader, int ordinal)
{
// Given a DataTableReader, display schema
// information about a particular column.
try
{
DataTable schemaTable = reader.GetSchemaTable();
DataRow row = schemaTable.Rows[ordinal];
foreach (DataColumn col in schemaTable.Columns)
{
Console.WriteLine("{0}: {1}",
col.ColumnName, row[col.Ordinal]);
}
}
catch (IndexOutOfRangeException ex)
{
Console.WriteLine("{0} is an invalid column number.",
ordinal);
}
}
private static SqlDataAdapter SetupDataAdapter(String sqlString)
{
// Assuming all the default settings, create a
// SqlDataAdapter working with the AdventureWorks
// sample database that's available with
// SQL Server.
String connectionString =
"Data source=(local);initial catalog=AdventureWorks;" +
"Integrated Security=True";
return new SqlDataAdapter(sqlString, connectionString);
}
Private Sub TestGetSchemaTable()
' Set up the data adapter, using information from
' the AdventureWorks sample database.
' Modify the SQL expression to retrieve
' data from a different table.
Dim adapter As SqlDataAdapter = _
SetupDataAdapter("SELECT * FROM Sales.Customer")
' Fill the DataTable, retrieving all the schema information.
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim table As New DataTable
adapter.Fill(table)
' Create the DataTableReader, and close it when done.
Using reader As New DataTableReader(table)
' Modify the column number to display information
' about a column other than column 0.
DisplaySchemaTableInfo(reader, 0)
End Using
Console.WriteLine()
Console.WriteLine("Press Enter to finish.")
Console.ReadLine()
End Sub
Private Sub DisplaySchemaTableInfo( _
ByVal reader As DataTableReader, ByVal ordinal As Integer)
' Given a DataTableReader, display schema
' information about a particular column.
Try
Dim schemaTable As DataTable = reader.GetSchemaTable()
Dim row As DataRow = schemaTable.Rows(ordinal)
For Each col As DataColumn In schemaTable.Columns
Console.WriteLine("{0}: {1}", _
col.ColumnName, row(col.Ordinal))
Next
Catch ex As IndexOutOfRangeException
Console.WriteLine("{0} is an invalid column number.", _
ordinal)
End Try
End Sub
Private Function SetupDataAdapter( _
ByVal sqlString As String) As SqlDataAdapter
' Assuming all the default settings, create a SqlDataAdapter
' working with the AdventureWorks sample database that's
' available with SQL Server.
Dim connectionString As String = _
"Data Source=(local);" & _
"Initial Catalog=AdventureWorks;" & _
"Integrated Security=true"
Return New SqlDataAdapter(sqlString, connectionString)
End Function
Remarks
The GetSchemaTable method returns metadata about each column in the following order:
DataReader column | Description |
---|---|
ColumnName | The name of the column as it appears in the DataTable. |
ColumnOrdinal | The ordinal of the column |
ColumnSize | -1 if the ColumnSize (or MaxLength) property of the DataColumn cannot be determined or is not relevant; otherwise, 0 or a positive integer that contains the MaxLength value. |
NumericPrecision | If the column type is a numeric type, this is the maximum precision of the column. If the column type is not a numeric data type, this is a null value. |
NumericScale | If column data type has a scale component, return the number of digits to the right of the decimal point. Otherwise, return a null value. |
DataType | The underlying type of the column. |
ProviderType | The indicator of the column's data type. If the data type of the column varies from row to row, this value is Object. This column cannot contain a null value. |
IsLong | true if the data type of the column is String and its MaxLength property is -1. Otherwise, false . |
AllowDBNull | true if the AllowDbNull constraint is set to true for the column; otherwise, false . |
IsReadOnly | true if the column cannot be modified; otherwise false . |
IsRowVersion | false , for every column. |
IsUnique | true : No two rows in the DataTable can have the same value in this column. IsUnique is guaranteed to be true if the column represents a key by itself or if there is a constraint of type UNIQUE that applies only to this column. false : The column can contain duplicate values in the DataTable . The default of this column is false . |
IsKey | true : The column is one of a set of columns that, taken together, uniquely identify the row in the DataTable. The set of columns with IsKey set to true must uniquely identify a row in the DataTable . There is no requirement that this set of columns is a minimal set of columns. This set of columns may be generated from a DataTable primary key, a unique constraint or a unique index. false : The column is not required to uniquely identify the row. This value is true if the column participates in a single or composite primary key. Otherwise, its value is false . |
IsAutoIncrement | true : The column assigns values to new rows in fixed increments. false : The column does not assign values to new rows in fixed increments. The default of this column is false . |
BaseCatalogName | The name of the catalog in the data store that contains the column. Null if the base catalog name cannot be determined. The default value for this column is a null value. |
BaseSchemaName | This value is always Null . |
BaseTableName | The name of the DataTable. |
BaseColumnName | The name of the column in the DataTable. |
AutoIncrementSeed | The value of the DataTable's AutoIncrementSeed property. |
AutoIncrementStep | The value of the DataTable's AutoIncrementStep property. |
DefaultValue | The value of the DataColumn's DefaultValue property. |
Expression | The expression string, if the current column is an expression column and all columns used in the expression belong to the same T:System.Data.DataTable that contains the expression column; otherwise null . |
ColumnMapping | The MappingType value associated with the DataColumn. The type can be one of Attribute , Element , Hidden , or SimpleContent . The default value is Element . |
BaseTableNamespace | The value of the DataTable's Namespace property. |
BaseColumnNamespace | The value of the DataColumn's Namespace property. |