vb获取数据库中所有表的名称
发布时间:2020-12-17 08:14:46 所属栏目:百科 来源:网络整理
导读:如何在 Visual Basic 中使用 ADO OpenSchema 方法 文章编号: 186246 - 查看本文应用于的产品 查看机器翻译免责声明 点击这里查看逐句中英文对照机器翻译 309488" _msthash="451464" _mstchunk="true"这篇文章的 Microsoft Visual Basic.NET 版本,请参阅3094
如何在 Visual Basic 中使用 ADO OpenSchema 方法
文章编号: 186246 -
查看本文应用于的产品
查看机器翻译免责声明
点击这里查看逐句中英文对照机器翻译
309488" _msthash="451464" _mstchunk="true">这篇文章的 Microsoft Visual Basic.NET 版本,请参阅309488
() http://support.microsoft.com/kb/309488/EN-US/
。
概要
本文介绍了如何使用 ActiveX 数据对象 (ADO) 连接对象的 OpenSchema 方法来获取有关数据库或表的详细信息。
下面是 OpenSchema 的语法:
设置记录集 = 连接。
OpenSchema (QueryType,条件 SchemaID)
回到顶端
|
提供反馈
更多信息
以下是 OpenSchema 方法的三个参数:
http://msdn2.microsoft.com/en-us/library/ms805098.aspx
() http://msdn2.microsoft.com/en-us/library/ms805098.aspx
QueryType Criteria ============================= adSchemaTables TABLE_CATALOG TABLE_SCHEMA TABLE_NAME TABLE_TYPE Microsoft Access 97 和 Access 2000若要列出的表和查询,Microsoft Access NWind 数据库中的所有操作,只需使用下面的代码:
Set rs = cn.OpenSchema(adSchemaTables) While Not rs.EOF Debug.Print rs!TABLE_NAME rs.MoveNext Wend
Set rs = cn.OpenSchema(adSchemaTables,_ Array(Empty,Empty,"Table") Microsoft SQL Server 6.5 和 7.0若要列出的所有表和 Microsoft SQL Server Pubs 数据库中的视图,请使用:
Set rs = cn.OpenSchema(adSchemaTables)
Set rs = cn.OpenSchema(adSchemaTables,_ Array("Pubs","Table") QueryType Criteria =============================== adSchemaColumns TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME Microsoft Access 97 和 Access 2000若要列出访问 Nwind adSchemaColumns 与数据库中雇员表中的字段,只需使用:
Set rs = cn.OpenSchema(adSchemaColumns,Array(Empty,"Employees") While Not rs.EOF Debug.Print rs!COLUMN_NAME rs.MoveNext Wend Microsoft SQL Server 6.5 和 7.0若要列出在 SQL Server Pubs 数据库中以 adSchemaColumns 作者表中的字段,只需使用:
Set rs = cn.OpenSchema(adSchemaColumns,Array("pubs","dbo","Authors") QueryType Criteria ================================ adSchemaIndexes TABLE_CATALOG TABLE_SCHEMA INDEX_NAME TYPE TABLE_NAME Microsoft Access 97 和 Access 2000若要列出访问 Nwind adSchemaIndexes 与数据库中雇员表中的索引,只需使用:
Set rs = cn.OpenSchema(adSchemaIndexes,"Employees") While Not rs.EOF Debug.Print rs!INDEX_NAME rs.MoveNext Wend Microsoft SQL Server 6.5 和 7.0若要列出在 SQL Server Pubs 数据库中以 adSchemaIndexes 作者表中的索引,只需使用:
Set rs = cn.OpenSchema(adSchemaIndexes,"Authors") OpenSchema 方法示例项目 菜单中选择 引用。在引用对话框中选择 Microsoft ActiveX 对象库。此示例使用与 SQL Server 一起提供的 Pubs 数据库。您需要将数据源名称 (DSN) 更改为您的计算机上的 DSN。将以下代码粘贴到的项目的常规声明节中:" _msthash="21495" _mstchunk="true">在 Visual Basic (VB),请选择标准 EXE 项目。EXE 项目中添加三个命令按钮。Project menu,choose References. " _msthash="64485" _mstchunk="true">从项目菜单中,选择引用。Microsoft ActiveX Objects Library. " _msthash="85980" _mstchunk="true">在引用对话框中,选择Microsoft ActiveX 对象库。此示例使用与 SQL Server 提供的 Pubs 数据库。您需要将数据源名称 (DSN) 更改为在您的计算机上的 DSN。将下面的代码粘贴到该项目的通用声明部分:<SPAN ? _mstsrchtml=" 注意您需要更改 UID = <username>和 PWD = < 强密码 > 到正确的值,在运行此代码之前。请确保 UID 具有适当的权限在数据库上执行此操作。" _msthash="21496" _mstchunk="true"> Note " _msthash="21496" _mstchunk="true">注意 您将需要更改 UID = <username> 和 PWD = < 强密码 > 为正确的值,您在运行此代码之前。 请确保该 UID 具有对数据库执行此操作的相应权限。
'Open the proper connection. Dim cn As New ADODB.Connection Dim rs As New ADODB.Recordset Private Sub Command1_Click() 'Getting the information about the columns in a particular table. Set rs = cn.OpenSchema(adSchemaColumns,_ "authors")) While Not rs.EOF Debug.Print rs!COLUMN_NAME rs.MoveNext Wend End Sub Private Sub Command2_Click() 'Getting the information about the primary key for a table. Set rs = cn.OpenSchema(adSchemaPrimaryKeys,_ "authors")) MsgBox rs!COLUMN_NAME End Sub Private Sub Command3_Click() 'Getting the information about all the tables. Dim criteria(3) As Variant criteria(0) = "pubs" criteria(1) = Empty criteria(2) = Empty criteria(3) = "table" Set rs = cn.OpenSchema(adSchemaTables,criteria) While Not rs.EOF Debug.Print rs!TABLE_NAME rs.MoveNext Wend End Sub Private Sub Form_Load() cn.Open "dsn=pubs;uid=<username>;pwd=<strong password>;" 'To test with the Native Provider for SQL Server,comment the ' line above then uncomment the following line. Modify to use ' your server. 'cn.Open "Provider=SQLOLEDB;Data Source=<servername>;" & _ ' "User ID=sa;password=;" End Sub (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |