2011-06-23 298 views
14

有沒有辦法從.NET中獲取安裝在Windows XP機器上的ODBC驅動程序列表?.NET中的ODBC驅動程序列表

我基本上希望看到(在.NET)是什麼:

控制面板 - >管理工具 - >數據源(ODBC) - > 「驅動程序」 選項卡 。

回答

12

thisthis

基本上這裏系統存儲的ODBC驅動程序的信息 HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers

您可以使用此或類似的代碼,找出所安裝的ODBC驅動程序。此代碼基本上讀取來自注冊表的驅動程序信息

public static List<String> GetSystemDriverList() 
     { 
      List<string> names = new List<string>(); 
      // get system dsn's 
      Microsoft.Win32.RegistryKey reg = (Microsoft.Win32.Registry.LocalMachine).OpenSubKey("Software"); 
      if (reg != null) 
      { 
       reg = reg.OpenSubKey("ODBC"); 
       if (reg != null) 
       { 
        reg = reg.OpenSubKey("ODBCINST.INI"); 
        if (reg != null) 
        { 

         reg = reg.OpenSubKey("ODBC Drivers"); 
         if (reg != null) 
         { 
          // Get all DSN entries defined in DSN_LOC_IN_REGISTRY. 
          foreach (string sName in reg.GetValueNames()) 
          { 
           names.Add(sName); 
          } 
         } 
         try 
         { 
          reg.Close(); 
         } 
         catch { /* ignore this exception if we couldn't close */ } 
        } 
       } 
      } 

      return names; 
     } 
+0

這是一個很好的答案,但是,它將爲n如果你在登記註冊時使用了'''''''冰塊。如果它崩潰,內存將被清理。 – billybob

11

沒有必要打開每個中間子項。讀取註冊表項,以獲得ODBC驅動程序名稱可以在更緊湊的方式進行如下:

/// <summary> 
    /// Gets the ODBC driver names from the registry. 
    /// </summary> 
    /// <returns>a string array containing the ODBC driver names, if the registry key is present; null, otherwise.</returns> 
    public static string[] GetOdbcDriverNames() 
    { 
     string[] odbcDriverNames = null; 
     using (RegistryKey localMachineHive = Registry.LocalMachine) 
     using (RegistryKey odbcDriversKey = localMachineHive.OpenSubKey(@"SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers")) 
     { 
      if (odbcDriversKey != null) 
      { 
       odbcDriverNames = odbcDriversKey.GetValueNames(); 
      } 
     } 

     return odbcDriverNames; 
    } 

您也可以通過執行在P實現的功能/ Invoke來SQLGetInstalledDriversW:

[DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)] 
    private static extern bool SQLGetInstalledDriversW(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut); 

    /// <summary> 
    /// Gets the ODBC driver names from the SQLGetInstalledDrivers function. 
    /// </summary> 
    /// <returns>a string array containing the ODBC driver names, if the call to SQLGetInstalledDrivers was successfull; null, otherwise.</returns> 
    public static string[] GetOdbcDriverNames() 
    { 
     string[] odbcDriverNames = null; 
     char[] driverNamesBuffer = new char[ushort.MaxValue]; 
     ushort size; 

     bool succeeded = SQLGetInstalledDriversW(driverNamesBuffer, ushort.MaxValue, out size); 

     if (succeeded == true) 
     { 
      char[] driverNames = new char[size - 1]; 
      Array.Copy(driverNamesBuffer, driverNames, size - 1); 
      odbcDriverNames = (new string(driverNames)).Split('\0'); 
     } 

     return odbcDriverNames; 
    } 

我也調用函數和使用結果如下創建ODBC數據源時正常降級到SQL驅動程序的早期版本:

/// <summary> 
    /// Gets the name of an ODBC driver for Microsoft SQL Server giving preference to the most recent one. 
    /// </summary> 
    /// <returns>the name of an ODBC driver for Microsoft SQL Server, if one is present; null, otherwise.</returns> 
    public static string GetOdbcSqlDriverName() 
    { 
     List<string> driverPrecedence = new List<string>() { "SQL Server Native Client 11.0", "SQL Server Native Client 10.0", "SQL Server Native Client 9.0", "SQL Server" }; 
     string[] availableOdbcDrivers = GetOdbcDriverNames(); 
     string driverName = null; 

     if (availableOdbcDrivers != null) 
     { 
      driverName = driverPrecedence.Intersect(availableOdbcDrivers).FirstOrDefault(); 
     } 

     return driverName; 
    } 
+0

那麼爲什麼不簡單地使用(註冊表鍵odbcDriversKey = Registry.LocalMachine.OpenSubKey(@「SOFTWARE \ ODBC \ ODBCINST.INI \ ODBC Drivers」))? :) – VladL

+0

@VladL對象處置。引用Registry.LocalMachine會生成一個實現IDisposable的對象。確保在完成時調用.Dispose是一種很好的做法,而不是等待垃圾回收來識別廢棄的對象。 – JamieSee

+2

不要忘記在64位機器上,32位驅動程序單獨存儲在這裏: 'HKEY_LOCAL_MACHINE \ SOFTWARE \ Wow6432Node \ ODBC \ ODBCINST.INI \ ODBC Drivers' – CrazyTim