枚举计算机上设置的 DSN 列表

发布于 2024-07-06 18:47:46 字数 152 浏览 7 评论 0原文

我有一个 Excel 应用程序,我想在其中向用户显示数据源名称(即:DSN)的列表,以便他/她可以选择要使用的数据源。

希望一旦获得列表,我就可以轻松访问 DSN 属性以连接到适当的数据库。

请注意,我不想想要使用无 DSN 连接。

I have an Excel application in which I want to present the user with a list of the Data Source Names (ie: DSN's), whereby s/he can choose what data source to use.

Hopefully once I've got the list, I can easily access the DSN properties to connect to the appropriate database.

Please note, I do not want to use a DSN-less connection.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

两相知 2024-07-13 18:47:46

DSN 条目存储在注册表中的以下项中。

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources

这包含所有定义的 DSN 的列表。 这充当全局索引,每个 DSN 的具体详细信息存储在带有 DSN 名称的键中:

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI

从数据源 (ODBC) 控制面板小程序在用户 DSN 和系统 DSN 选项卡中创建一些条目,并检查这些值的情况存储在注册表中。

以下示例枚举通过“控制面板”>“用户”定义的 DSN。 管理工具> 数据源 (ODBC) [用户 Dsn 选项卡]。

http://support.microsoft.com/kb/178755

  Option Explicit

  Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
      Alias "RegOpenKeyExA" _
      (ByVal hKey As Long, _
      ByVal lpSubKey As String, _
      ByVal ulOptions As Long, _
      ByVal samDesired As Long, phkResult As Long) As Long

  Private Declare Function RegEnumValue Lib "advapi32.dll" _
      Alias "RegEnumValueA" _
      (ByVal hKey As Long, _
      ByVal dwIndex As Long, _
      ByVal lpValueName As String, _
      lpcbValueName As Long, _
      ByVal lpReserved As Long, _
      lpType As Long, _
      lpData As Any, _
      lpcbData As Long) As Long

  Private Declare Function RegCloseKey Lib "advapi32.dll" _
      (ByVal hKey As Long) As Long

  Const HKEY_CLASSES_ROOT = &H80000000
  Const HKEY_CURRENT_USER = &H80000001
  Const HKEY_LOCAL_MACHINE = &H80000002
  Const HKEY_USERS = &H80000003

  Const ERROR_SUCCESS = 0&

  Const SYNCHRONIZE = &H100000
  Const STANDARD_RIGHTS_READ = &H20000
  Const STANDARD_RIGHTS_WRITE = &H20000
  Const STANDARD_RIGHTS_EXECUTE = &H20000
  Const STANDARD_RIGHTS_REQUIRED = &HF0000
  Const STANDARD_RIGHTS_ALL = &H1F0000
  Const KEY_QUERY_VALUE = &H1
  Const KEY_SET_VALUE = &H2
  Const KEY_CREATE_SUB_KEY = &H4
  Const KEY_ENUMERATE_SUB_KEYS = &H8
  Const KEY_NOTIFY = &H10
  Const KEY_CREATE_LINK = &H20
  Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
                    KEY_QUERY_VALUE Or _
                    KEY_ENUMERATE_SUB_KEYS Or _
                    KEY_NOTIFY) And _
                    (Not SYNCHRONIZE))

  Const REG_DWORD = 4
  Const REG_BINARY = 3
  Const REG_SZ = 1

  Private Sub Command1_Click()
     Dim lngKeyHandle As Long
     Dim lngResult As Long
     Dim lngCurIdx As Long
     Dim strValue As String
     Dim lngValueLen As Long
     Dim lngData As Long
     Dim lngDataLen As Long
     Dim strResult As String

     lngResult = RegOpenKeyEx(HKEY_CURRENT_USER, _
             "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", _
              0&, _
              KEY_READ, _
              lngKeyHandle)

     If lngResult <> ERROR_SUCCESS Then
         MsgBox "Cannot open key"
         Exit Sub
     End If

     lngCurIdx = 0
     Do
        lngValueLen = 2000
        strValue = String(lngValueLen, 0)
        lngDataLen = 2000

        lngResult = RegEnumValue(lngKeyHandle, _
                                 lngCurIdx, _
                                 ByVal strValue, _
                                 lngValueLen, _
                                 0&, _
                                 REG_DWORD, _
                                 ByVal lngData, _
                                 lngDataLen)
        lngCurIdx = lngCurIdx + 1

        If lngResult = ERROR_SUCCESS Then
           strResult = strResult & lngCurIdx & ": " & Left(strValue, lngValueLen) & vbCrLf
        End If
     Loop While lngResult = ERROR_SUCCESS
     Call RegCloseKey(lngKeyHandle)

     Call MsgBox(strResult, vbInformation)
  End Sub

The DSN entries are stored in the registry in the following keys.

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI\ODBC Data Sources
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources

This contains the list of all defined DSN. This acts as an global index and the specific details for each DSN are stored in a key with the DSN name under:

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBC.INI

Create some entries in both User DSN and System DSN tabs from Data Sources (ODBC) control panel applet and check how these values are stored in the registry.

The following example enumerate the DSN defined for the user trough Control Panel > Administrative Tools > Data Sources (ODBC) [User Dsn Tab].

http://support.microsoft.com/kb/178755

  Option Explicit

  Private Declare Function RegOpenKeyEx Lib "advapi32.dll" _
      Alias "RegOpenKeyExA" _
      (ByVal hKey As Long, _
      ByVal lpSubKey As String, _
      ByVal ulOptions As Long, _
      ByVal samDesired As Long, phkResult As Long) As Long

  Private Declare Function RegEnumValue Lib "advapi32.dll" _
      Alias "RegEnumValueA" _
      (ByVal hKey As Long, _
      ByVal dwIndex As Long, _
      ByVal lpValueName As String, _
      lpcbValueName As Long, _
      ByVal lpReserved As Long, _
      lpType As Long, _
      lpData As Any, _
      lpcbData As Long) As Long

  Private Declare Function RegCloseKey Lib "advapi32.dll" _
      (ByVal hKey As Long) As Long

  Const HKEY_CLASSES_ROOT = &H80000000
  Const HKEY_CURRENT_USER = &H80000001
  Const HKEY_LOCAL_MACHINE = &H80000002
  Const HKEY_USERS = &H80000003

  Const ERROR_SUCCESS = 0&

  Const SYNCHRONIZE = &H100000
  Const STANDARD_RIGHTS_READ = &H20000
  Const STANDARD_RIGHTS_WRITE = &H20000
  Const STANDARD_RIGHTS_EXECUTE = &H20000
  Const STANDARD_RIGHTS_REQUIRED = &HF0000
  Const STANDARD_RIGHTS_ALL = &H1F0000
  Const KEY_QUERY_VALUE = &H1
  Const KEY_SET_VALUE = &H2
  Const KEY_CREATE_SUB_KEY = &H4
  Const KEY_ENUMERATE_SUB_KEYS = &H8
  Const KEY_NOTIFY = &H10
  Const KEY_CREATE_LINK = &H20
  Const KEY_READ = ((STANDARD_RIGHTS_READ Or _
                    KEY_QUERY_VALUE Or _
                    KEY_ENUMERATE_SUB_KEYS Or _
                    KEY_NOTIFY) And _
                    (Not SYNCHRONIZE))

  Const REG_DWORD = 4
  Const REG_BINARY = 3
  Const REG_SZ = 1

  Private Sub Command1_Click()
     Dim lngKeyHandle As Long
     Dim lngResult As Long
     Dim lngCurIdx As Long
     Dim strValue As String
     Dim lngValueLen As Long
     Dim lngData As Long
     Dim lngDataLen As Long
     Dim strResult As String

     lngResult = RegOpenKeyEx(HKEY_CURRENT_USER, _
             "SOFTWARE\ODBC\ODBC.INI\ODBC Data Sources", _
              0&, _
              KEY_READ, _
              lngKeyHandle)

     If lngResult <> ERROR_SUCCESS Then
         MsgBox "Cannot open key"
         Exit Sub
     End If

     lngCurIdx = 0
     Do
        lngValueLen = 2000
        strValue = String(lngValueLen, 0)
        lngDataLen = 2000

        lngResult = RegEnumValue(lngKeyHandle, _
                                 lngCurIdx, _
                                 ByVal strValue, _
                                 lngValueLen, _
                                 0&, _
                                 REG_DWORD, _
                                 ByVal lngData, _
                                 lngDataLen)
        lngCurIdx = lngCurIdx + 1

        If lngResult = ERROR_SUCCESS Then
           strResult = strResult & lngCurIdx & ": " & Left(strValue, lngValueLen) & vbCrLf
        End If
     Loop While lngResult = ERROR_SUCCESS
     Call RegCloseKey(lngKeyHandle)

     Call MsgBox(strResult, vbInformation)
  End Sub
无言温柔 2024-07-13 18:47:46

您可以使用 ODBC API 的SQLDataSources 函数。 请参阅 MSDN 文档

You can use the SQLDataSources function of the ODBC API. See MSDN documentation.

你怎么敢 2024-07-13 18:47:46

非常酷的解决方案。 我遇到了一个问题,CURRENT_USER 没有显示所有 DSN,当然不是我需要的 DSN。 我将其更改为 LOCAL_MACHINE,并看到连接管理器中显示的所有 DSN,包括 CURRENT_USER 下显示的子集。

http://msdn.microsoft。 com/en-us/library/windows/desktop/ms712603(v=vs.85).aspx

Extremely cool solution. I ran into an issue where CURRENT_USER wasn't showing all the DSN's, certainly not the one I needed. I changed it to LOCAL_MACHINE and saw all DSN's that showed up in the Connection Manager, including the subset that showed up under CURRENT_USER.

http://msdn.microsoft.com/en-us/library/windows/desktop/ms712603(v=vs.85).aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文