OdbcDataReader.GetSchemaTable OdbcDataReader.GetSchemaTable OdbcDataReader.GetSchemaTable OdbcDataReader.GetSchemaTable Method

定义

返回一个 DataTable,它描述 OdbcDataReader 的列元数据。Returns a DataTable that describes the column metadata of the OdbcDataReader.

public:
 virtual System::Data::DataTable ^ GetSchemaTable();
public System.Data.DataTable GetSchemaTable ();
override this.GetSchemaTable : unit -> System.Data.DataTable
Public Function GetSchemaTable () As DataTable

返回

一个描述列元数据的 DataTableA DataTable that describes the column metadata.

异常

注解

GetSchemaTable方法按以下顺序返回有关每个列的元数据:The GetSchemaTable method returns metadata about each column in the following order:

DataReader 列DataReader column 说明Description
列名ColumnName 列的名称;这可能不是唯一的。The name of the column; this might not be unique. 如果无法确定列名,则返回 null 值。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.
ColumnOrdinalColumnOrdinal 从零开始的列序号。The zero-based ordinal of the column. 此列不能包含 null 值。This column cannot contain a null value.
ColumnSizeColumnSize 列中的值的最大可能长度。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.
NumericPrecisionNumericPrecision 如果DbType是数值数据类型,则这是列的最大精度。If DbType is a numeric data type, this is the maximum precision of the column. 精度取决于该列的定义。The precision depends on the definition of the column. 如果DbType不是数值数据类型,请不要使用此列中的数据。If DbType is not a numeric data type, do not use the data in this column. 如果基础 ODBC 驱动程序返回非数值数据类型的精度值,则会在架构表中使用此值。If the underlying ODBC driver returns a precision value for a non-numeric data type, this value is used in the schema table.
NumericScaleNumericScale 如果DbTypeDecimal,则为小数点右侧的位数。If DbType is Decimal, the number of digits to the right of the decimal point. 否则,此值为 null 值。Otherwise, this is a null value. 如果基础 ODBC 驱动程序返回非数值数据类型的精度值,则会在架构表中使用此值。If the underlying ODBC driver returns a precision value for a non-numeric data type, this value is used in the schema table.
数据类型DataType 映射到的公共语言运行时类型DbTypeMaps to the common language runtime type of DbType.
ProviderTypeProviderType 基础驱动程序类型。The underlying driver type.
IsLongIsLong true如果列包含包含非常长数据的二进制长对象(BLOB),则为。true if the column contains a Binary Long Object (BLOB) that contains very long data. 非常长的数据的定义是驱动程序特定的。The definition of very long data is driver-specific.
AllowDBNullAllowDBNull true如果使用者可以将列设置为 null 值,或者如果驱动程序无法确定使用者是否可以将该列设置为 null 值,则为。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. 否则为 falseOtherwise, false. 列可能包含空值,即使它不能设置为 null 值也是如此。A column may contain null values, even if it cannot be set to a null value.
为只读IsReadOnly true如果无法修改列,则为;否则false为。true if the column cannot be modified; otherwise false.
IsRowVersionIsRowVersion 如果列包含不能写入的持久性行标识符,并且没有任何有意义的值(标识该行),则设置。Set if the column contains a persistent row identifier that cannot be written to, and has no meaningful value except to identity the row.
IsUniqueIsUnique true:基表中没有两行(在 BaseTableName 中返回的表)在此列中可能具有相同的值。true: No two rows in the base table (the table returned in BaseTableName) can have the same value in this column. 如果列本身表示键true ,或者存在仅适用于此列的 UNIQUE 类型的约束,则可保证 IsUnique 为。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:列可以包含基表中的重复值。false: The column can contain duplicate values in the base table. 此列的默认值为falseThe default for this column is false.
IsKeyIsKey true:此列是行集中的一组列中的一组列,这些列一起使用,可唯一地标识行。true: The column is one of a set of columns in the rowset that, taken together, uniquely identify the row. 将 IsKey 设置为true的列集必须唯一标识行集中的行。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. 这组列可以从基表主键、unique 约束或唯一索引生成。This set of columns may be generated from a base table primary key, a unique constraint, or a unique index.

false:列不需要唯一地标识行。false: The column is not required to uniquely identify the row.
IsAutoIncrementIsAutoIncrement true如果列以固定增量将值分配给新行,则为;否则false为。true if the column assigns values to new rows in fixed increments; otherwise false. 此列的默认值为falseThe default for this column is false.
BaseSchemaNameBaseSchemaName 数据源中包含该列的架构的名称。The name of the schema in the data source that contains the column. 如果无法确定基本目录名称,则为 NULL。NULL if the base catalog name cannot be determined. 此列的默认值为 null 值。The default for this column is a null value.
BaseCatalogNameBaseCatalogName 包含该列的数据存储区中的目录的名称。The name of the catalog in the data store that contains the column. 如果无法确定基本目录名称,则为 NULL。NULL if the base catalog name cannot be determined. 此列的默认值为 null 值。The default for this column is a null value.
BaseTableNameBaseTableName 包含该列的数据存储区中的表或视图的名称。The name of the table or view in the data store that contains the column. 如果无法确定基表名称,则为 null 值。A null value if the base table name cannot be determined. 此列的默认值为 null 值。The default of this column is a null value.
BaseColumnNameBaseColumnName 数据存储区中列的名称。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. 如果无法确定基列名称,或者如果行集列派生,但与数据存储区中的列不完全相同,则为 null 值。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. 此列的默认值为 null 值。The default for this column is a null value.

为结果集中的每一列返回一行。A row is returned for every column in the results set.

ODBC 的 .NET Framework 数据提供程序假定在调用SQLPrepareSQLExecuteSQLEXECUTEDIRECT函数之一后,odbc 驱动程序中提供了元数据信息。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. 要使 "SchemaOnly" 命令行为正常工作, SQLPrepare必须返回所需的元数据信息。For "SchemaOnly" command behavior to work correctly, SQLPrepare must return the required metadata information. 并非所有 ODBC 驱动程序都支持此函数或返回元数据信息。Not all ODBC drivers support this function or return metadata information. 在这些情况下,SchemaTable 中的部分或全部信息将丢失。In these cases, part or all of the information in the SchemaTable will be missing. 调用SQLPrepare后,数据访问接口会调用 ODBC SQLColAttribute函数来查找与查询结果中的每个列相关的元数据信息(例如,IsLong、IsUnique、AllowDBNull、BaseTableName、BaseColumnName)。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). 如果基础驱动程序未返回某些信息,则不会正确设置 SchemaTable 中的相应值。If the underlying driver does not return some of this information, the corresponding values in the SchemaTable will not be set correctly.

用于 ODBC 的 .NET Framework 数据提供程序还将调用SQLPrimaryKeys来检索每个表的键信息。The .NET Framework Data Provider for ODBC also calls SQLPrimaryKeys to retrieve the key information for every table. 如果基础 ODBC 驱动程序不支持此函数,则数据访问接口将调用SQLStatistics并选择一个唯一索引作为该表的主键。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需要表的主键的正确标识,才能正常工作。OdbcCommandBuilder needs the correct identification of the primary keys of the table in order to work correctly. 如果查询结果中的每一列都未返回 BaseTableName,则用于 ODBC 的 .NET Framework 数据提供程序将尝试分析 SQL 语句,以便查找查询中涉及的表名称。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. 这适用于更新、插入、删除和简单的 SELECT 语句,但不适用于存储过程或基于联接的 SELECT 语句。This works with UPDATE, INSERT, DELETE and simple SELECT statements, but not with stored procedures or SELECT statements based on joins. 如果此表中缺少部分或全部架构信息, OdbcCommandBuilder将无法正常工作,因为它没有足够的架构信息来自动生成正确的 INSERT、UPDATE 或 DELETE 语句。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.

若要确保 metadata 列返回正确的信息,必须调用ExecuteReader并将行为参数设置为 KeyInfo。To make sure that metadata columns return the correct information, you must call ExecuteReader with the behavior parameter set to KeyInfo. 否则,架构表中的某些列可能返回默认值、null 值或不正确的数据。Otherwise, some of the columns in the schema table may return default, null, or incorrect data.

通过 ODBC 驱动程序使用 ODBC.NET 到 Oracle 时,别名列不会被识别为键列。When you use ODBC.NET to Oracle via the ODBC driver, aliased key columns are not recognized as keys. 这会影响 OdbcDataReader 的架构表中的 IsKey 和 IsUnique 列。This affects the IsKey and IsUnique columns in the schema table of the OdbcDataReader. 它还会影响 OdbcCommandBuilder 生成更新逻辑的能力。It also affects the OdbcCommandBuilder's ability to generate updating logic. 请考虑不要对主键列使用别名。Consider not using an alias for a primary key column.

备注

Microsoft Jet ODBC 驱动程序始终将唯一索引和主键列返回为 null,无论它们是否可以为 null。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.

适用于

另请参阅