2011-06-23 58 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