0

我在我的机器上也有 DSN 的列表

    private IEnumerable<string> EnumDsn(Microsoft.Win32.RegistryKey rootKey)
{
    Microsoft.Win32.RegistryKey regKey =  rootKey.OpenSubKey(@"Software\ODBC\ODBC.INI\ODBC Data Sources");
    if (regKey != null)
    {
        foreach (string name in regKey.GetValueNames())
        {
            string value = regKey.GetValue(name, "").ToString();
            yield return name;
        }
    }
}

我的意图是获取每个 DSN 的驱动程序类型,如何获取它。

4

1 回答 1

0

您可以从 odbccp32.dll 调用SQLGetPrivateProfileString 函数来检索您正在寻找的信息。

"ODBC Data Sources"用作 lpszSection 的参数,您的数据源名称 (DSN) 作为 lpszEntry 的参数。缓冲区中返回的值将是您的驱动程序名称。

在 C# 中导入 DLL 和函数如下所示:

    [DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int SQLGetPrivateProfileStringW(string lpszSection, string lpszEntry, string lpszDefault, char[] RetBuffer, int cbRetBuffer, string lpszFilename);

在 VB.NET 中看起来像这样:

    <DllImport("odbccp32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
    Private Shared Function SQLGetPrivateProfileStringW(lpszSection As String, lpszEntry As String, RetBuffer As Char(), cbRetBuffer As Integer, lpszFilename As String) As Integer
于 2014-01-15T16:55:10.693 回答