OpenSchema 方法范例

该范例使用 OpenSchema 方法显示 Pubs 数据库内每个表的名称和类型。

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 数据库中指定视图的模式信息。然后该范例显示每个表的名称和类型。

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
www.holmesian.org