OdbcDataReader.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 OdbcDataReader.
public:
override System::Data::DataTable ^ GetSchemaTable();
public:
virtual System::Data::DataTable ^ GetSchemaTable();
public override System.Data.DataTable? GetSchemaTable ();
public System.Data.DataTable GetSchemaTable ();
public override System.Data.DataTable GetSchemaTable ();
override this.GetSchemaTable : unit -> System.Data.DataTable
abstract member GetSchemaTable : unit -> System.Data.DataTable
override this.GetSchemaTable : unit -> System.Data.DataTable
Public Overrides Function GetSchemaTable () As DataTable
Public Function GetSchemaTable () As DataTable
Returns
A DataTable that describes the column metadata.
Implements
Exceptions
The OdbcDataReader is closed.
Remarks
The GetSchemaTable method returns metadata about each column in the following order:
DataReader column | Description |
---|---|
ColumnName | The name of the column; this might not be unique. If the column name cannot be determined, a null value is returned. This name always reflects the most recent naming of the column in the current view or command text. |
ColumnOrdinal | The zero-based ordinal of the column. This column cannot contain a null value. |
ColumnSize | The maximum possible length of a value in the column. For columns that use a fixed-length data type, this is the size of the data type. |
NumericPrecision | If DbType is a numeric data type, this is the maximum precision of the column. The precision depends on the definition of the column. If DbType is not a numeric data type, do not use the data in this column. If the underlying ODBC driver returns a precision value for a non-numeric data type, this value is used in the schema table. |
NumericScale | If DbType is Decimal, the number of digits to the right of the decimal point. Otherwise, this is a null value. If the underlying ODBC driver returns a precision value for a non-numeric data type, this value is used in the schema table. |
DataType | Maps to the common language runtime type of DbType. |
ProviderType | The underlying driver type. |
IsLong | true if the column contains a Binary Long Object (BLOB) that contains very long data. The definition of very long data is driver-specific. |
AllowDBNull | true if the consumer can set the column to a null value or if the driver cannot determine whether the consumer can set the column to a null value. Otherwise, false . A column may contain null values, even if it cannot be set to a null value. |
IsReadOnly | true if the column cannot be modified; otherwise false . |
IsRowVersion | Set if the column contains a persistent row identifier that cannot be written to, and has no meaningful value except to identity the row. |
IsUnique | true : No two rows in the base table (the table returned in BaseTableName) 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 base table. The default for this column is false . |
IsKey | true : The column is one of a set of columns in the rowset that, taken together, uniquely identify the row. The set of columns with IsKey set to true must uniquely identify a row in the rowset. There is no requirement that this set of columns is a minimal set of columns. This set of columns may be generated from a base table primary key, a unique constraint, or a unique index.false : The column is not required to uniquely identify the row. |
IsAutoIncrement | true if the column assigns values to new rows in fixed increments; otherwise false . The default for this column is false . |
BaseSchemaName | The name of the schema in the data source that contains the column. NULL if the base catalog name cannot be determined. The default for this column is a null value. |
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 for this column is a null value. |
BaseTableName | The name of the table or view in the data store that contains the column. A null value if the base table name cannot be determined. The default of this column is a null value. |
BaseColumnName | The name of the column in the data store. This might be different from the column name returned in the ColumnName column if an alias was used. A null value if the base column name cannot be determined or if the rowset column is derived, but not identical to, a column in the data store. The default for this column is a null value. |
A row is returned for every column in the results set.
The .NET Framework Data Provider for ODBC assumes that metadata information is available from an ODBC driver after one of SQLPrepare, SQLExecute, or SQLExecuteDirect functions are called. For "SchemaOnly" command behavior to work correctly, SQLPrepare must return the required metadata information. Not all ODBC drivers support this function or return metadata information. In these cases, part or all of the information in the SchemaTable will be missing. After calling SQLPrepare, The data provider calls the ODBC SQLColAttribute function to find the metadata information related to each column in the query results (for example, IsLong, IsUnique, AllowDBNull, BaseTableName, BaseColumnName). If the underlying driver does not return some of this information, the corresponding values in the SchemaTable will not be set correctly.
The .NET Framework Data Provider for ODBC also calls SQLPrimaryKeys to retrieve the key information for every table. If the underlying ODBC driver does not support this function, the data provider calls SQLStatistics and chooses one of the unique indexes as the primary key for the table. This may not always give the results that you want.
OdbcCommandBuilder needs the correct identification of the primary keys of the table in order to work correctly. If the BaseTableName is not returned for every column in the query results, the .NET Framework Data Provider for ODBC tries to parse the SQL statement to find the table names involved in the query. This works with UPDATE, INSERT, DELETE and simple SELECT statements, but not with stored procedures or SELECT statements based on joins. If some or all the schema information is missing from this table, the OdbcCommandBuilder will not work correctly, because it has insufficient schema information to automatically generate the correct INSERT, UPDATE, or DELETE statements.
To make sure that metadata columns return the correct information, you must call ExecuteReader with the behavior parameter set to KeyInfo. Otherwise, some of the columns in the schema table may return default, null, or incorrect data.
When you use ODBC.NET to Oracle via the ODBC driver, aliased key columns are not recognized as keys. This affects the IsKey and IsUnique columns in the schema table of the OdbcDataReader. It also affects the OdbcCommandBuilder's ability to generate updating logic. Consider not using an alias for a primary key column.
Note
The Microsoft Jet ODBC driver always returns unique index and primary key columns as nullable regardless of whether they are nullable or not. The driver also does not return primary key information; it only returns a list of unique indexes and their columns, including primary key columns, without differentiating among them.