从提供者获取数据库纲要信息。
语法
Set recordset = connection.OpenSchema (QueryType, Criteria, SchemaID)
返回值
返回包含纲要信息的 Recordset 对象。Recordset 将以只读、静态游标打开。
参数
QueryType 所要运行的纲要查询类型,可以为下列任意常量。
Criteria 可选。每个 QueryType 选项的查询限制条件数组如下所列。
QueryType 值 | Criteria 值 |
adSchemaAsserts | CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME |
adSchemaCatalogs | CATALOG_NAME |
adSchemaCharacterSets | CHARACTER_SET_CATALOG CHARACTER_SET_SCHEMA CHARACTER_SET_NAME |
adSchemaCheckConstraints | CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME |
adSchemaCollations | COLLATION_CATALOG COLLATION_SCHEMA COLLATION_NAME |
adSchemaColumnDomainUsage | DOMAIN_CATALOG DOMAIN_SCHEMA DOMAIN_NAME COLUMN_NAME |
adSchemaColumnPrivileges | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME GRANTOR GRANTEE |
adSchemaColumns | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME |
adSchemaConstraintColumnUsage | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME |
adSchemaConstraintTableUsage | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME |
adSchemaForeignKeys | PK_TABLE_CATALOG PK_TABLE_SCHEMA PK_TABLE_NAME FK_TABLE_CATALOG FK_TABLE_SCHEMA FK_TABLE_NAME |
adSchemaIndexes | TABLE_CATALOG TABLE_SCHEMA INDEX_NAME TYPE TABLE_NAME |
adSchemaKeyColumnUsage | CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME |
adSchemaPrimaryKeys | PK_TABLE_CATALOG PK_TABLE_SCHEMA PK_TABLE_NAME |
adSchemaProcedureColumns | PROCEDURE_CATALOG PROCEDURE_SCHEMA PROCEDURE_NAME COLUMN_NAME |
adSchemaProcedureParameters | PROCEDURE_CATALOG PROCEDURE_SCHEMA PROCEDURE_NAME PARAMTER_NAME |
adSchemaProcedures | PROCEDURE_CATALOG PROCEDURE_SCHEMA PROCEDURE_NAME PARAMTER_TYPE |
adSchemaProviderSpecific | 参见说明 |
adSchemaProviderTypes | DATA_TYPE BEST_MATCH |
adSchemaReferentialConstraints | CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME |
adSchemaSchemata | CATALOG_NAME SCHEMA_NAME SCHEMA_OWNER |
adSchemaSQLLanguages | <无> |
adSchemaStatistics | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME |
adSchemaTableConstraints | CONSTRAINT_CATALOG CONSTRAINT_SCHEMA CONSTRAINT_NAME TABLE_CATALOG TABLE_SCHEMA TABLE_NAME CONSTRAINT_TYPE |
adSchemaTablePrivileges | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME GRANTOR GRANTEE |
adSchemaTables | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE |
adSchemaTranslations | TRANSLATION_CATALOG TRANSLATION_SCHEMA TRANSLATION_NAME |
adSchemaUsagePrivileges | OBJECT_CATALOG OBJECT_SCHEMA OBJECT_NAME OBJECT_TYPE GRANTOR GRANTEE |
adSchemaViewColumnUsage | VIEW_CATALOG VIEW_SCHEMA VIEW_NAME |
adSchemaViewTableUsage | VIEW_CATALOG VIEW_SCHEMA VIEW_NAME |
adSchemaViews | TABLE_CATALOG TABLE_SCHEMA TABLE_NAME |
SchemaID OLE DB 规范没有定义提供者纲要查询的 GUID。如果 QueryType 设置为 adSchemaProviderSpecific,则需要该参数,否则不使用它。
说明
OpenSchema 方法返回与数据源有关的信息,如关于服务器上的表以及表中的列等信息。
Criteria 参数是可用于限制纲要查询结果的值数组。每个纲要查询有它支持的不同参数集。实际纲要由 IDBSchemaRowset 接口下的 OLE DB 规范定义。ADO 中所支持的参数集已在上面列出。
如果提供者定义未在上面列出的非标准纲要查询,则常量 adSchemaProviderSpecific 将用于 QueryType 参数。在使用该常量时需要 SchemaID 参数传递纲要查询的 GUID 以用于执行。如果 QueryType 设置为 adSchemaProviderSpecific 但是没有提供 SchemaID,将导致错误。
提供者不需要支持所有的 OLE DB 标准纲要查询,只有 adSchemaTables、adSchemaColumns 和 adSchemaProviderTypes 是 OLE DB 规范所需要的。但是对于这些纲要查询,提供者不需要支持上面列出的 Criteria 条件约束。
Remote Data Service 用法 OpenSchema 方法在客户端 Connection 对象上无效。
=========================
Public Sub OpenSchemaX()
Dim cnn1 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn1 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn1.Open strCnn
Set rstSchema = cnn1.OpenSchema(adSchemaTables)
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn1.Close
End Sub
该范例在 OpenSchema 方法的 Criteria 参数中指定 TABLE_TYPE 查询约束。因此,只返回在 Pubs 数据库中指定的 Views 的纲要信息。然后该范例显示这些表的名称和类型。
Public Sub OpenSchemaX2()
Dim cnn2 As ADODB.Connection
Dim rstSchema As ADODB.Recordset
Dim strCnn As String
Set cnn2 = New ADODB.Connection
strCnn = "Provider=sqloledb;" & _
"Data Source=srv;Initial Catalog=pubs;User Id=sa;Password=; "
cnn2.Open strCnn
Set rstSchema = cnn2.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "VIEW"))
Do Until rstSchema.EOF
Debug.Print "Table name: " & _
rstSchema!TABLE_NAME & vbCr & _
"Table type: " & rstSchema!TABLE_TYPE & vbCr
rstSchema.MoveNext
Loop
rstSchema.Close
cnn2.Close
End Sub