.NET中的ODBC驱动程序列表

有没有办法从.NET获取安装在Windows XP计算机上的ODBC驱动程序列表?

我基本上希望看到(在.NET中)的内容:

控制面板 – >管理工具 – >数据源(ODBC) – >“驱动程序”选项卡。

看到这个或这个

基本上系统将ODBC驱动程序的信息存储在HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers

您可以使用此代码或类似代码来查找已安装的ODBC驱动程序。 此代码基本上从注册表中读取驱动程序信息

  public static List GetSystemDriverList() { List names = new List(); // 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; } 

没有必要打开每个中间子项。 读取注册表项以获取ODBC驱动程序名称可以以更紧凑的方式完成,如下所示:

  ///  /// Gets the ODBC driver names from the registry. ///  /// a string array containing the ODBC driver names, if the registry key is present; null, otherwise. 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; } 

您还可以通过对SQLGetInstalledDriversW执行P / Invoke来实现该function:

  [DllImport("odbccp32.dll", CharSet = CharSet.Unicode, SetLastError = true)] private static extern bool SQLGetInstalledDriversW(char[] lpszBuf, ushort cbufMax, out ushort pcbBufOut); ///  /// Gets the ODBC driver names from the SQLGetInstalledDrivers function. ///  /// a string array containing the ODBC driver names, if the call to SQLGetInstalledDrivers was successfull; null, otherwise. 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驱动程序的早期版本:

  ///  /// Gets the name of an ODBC driver for Microsoft SQL Server giving preference to the most recent one. ///  /// the name of an ODBC driver for Microsoft SQL Server, if one is present; null, otherwise. public static string GetOdbcSqlDriverName() { List driverPrecedence = new List() { "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; }