如何遍历ADODB连接并列出表名,字段名和字段类型?

时间:2017-07-18 18:52:37

标签: vba excel-vba excel

在做了一些谷歌搜索之后,我能够将以下脚本放在一起,其中列出了字段名称和数据类型,但没有列出表名称。我真的想列出表名,字段名和字段类型(一次向下运行一行)。此外,我宁愿循环连接字符串,而不是硬编码解决方案,因为我有一堆指向SQL Server的ADODB连接。

这是通过点击From Other Sources>设置的。从SQL Server>服务器名称>下一个 。 。 。 这就是我设置所有连接字符串的方式。

Sub DataExtract()

' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Connect to the Pubs database on the local server.
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=TABLE;Data Source=SERVER"

'Now open the connection.
cnPubs.Open strConn

' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset

i = 2
With rsPubs
    .ActiveConnection = cnPubs
    .Open "SELECT * FROM TABLE"
     For i = 1 To .Fields.Count
         Sheet1.Cells(i, 2) = .Fields(i - 1).Name
         Sheet1.Cells(i, 3) = .Fields(i - 1).Type
     Next i
End With

cnPubs.Close
Set rsPubs = Nothing
Set cnPubs = Nothing

End Sub

我会发布我的微弱脚本,希望它有所帮助。

2 个答案:

答案 0 :(得分:0)

查看openschema及其相关选项,您可以完成所有要求。

cnPubs.OpenSchema(adSchemaTables)

cnPubs.OpenScheam(adSchemaColumns)

有关选项,请参阅此链接。

http://www.netbox.cn/document/htm/mdcstschemaenum.htm

答案 1 :(得分:0)

我让这个工作。

Sub DataExtractFromSQL_Server()

' Create a connection object.
Dim cnPubs As ADODB.Connection
Set cnPubs = New ADODB.Connection

' Provide the connection string.
Dim strConn As String

'Connect to the Pubs database on the local server.
strConn = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Initial Catalog=DATABASE_NAME;Data Source=SERVER_NAME"

'Now open the connection.
' Create a recordset object.
Dim rsPubs As ADODB.Recordset
Set rsPubs = New ADODB.Recordset


Worksheets(1).Cells.Clear

Dim sht As Worksheet
Dim LastRow As Long

Set sht = ThisWorkbook.Worksheets("Data Validation")

LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row

cnt = ActiveWorkbook.Connections.Count

For j = cnt To 1 Step -1

    cnPubs.Open strConn
    Set conn = ActiveWorkbook.Connections.Item(j)
    ' conn is the name of the connection string; not the connection string
    Sql = "SELECT * FROM [TABLE_PREFIX_NAME].[dbo].[" & conn & "]"
    With rsPubs

        .ActiveConnection = cnPubs
        .Open Sql
         For i = 1 To .Fields.Count
             Worksheets(1).Cells(LastRow, 1) = conn
             Worksheets(1).Cells(LastRow, 2) = .Fields(i - 1).Name

             Select Case DataType
             Case .Fields(i - 1).Type = "200"
                Worksheets(1).Cells(LastRow, 3) = "VARCHAR"
             Case .Fields(i - 1).Type = "131"
                Worksheets(1).Cells(LastRow, 3) = "DECIMAL"
             Case .Fields(i - 1).Type = "135"
                Worksheets(1).Cells(LastRow, 3) = "DATE"
             Case Else
                Worksheets(1).Cells(LastRow, 3) = "INT"
             End Select

             LastRow = LastRow + 1
         Next i
    End With
    cnPubs.Close

Next j

Set rsPubs = Nothing
Set cnPubs = Nothing

End Sub