有没有办法从.NET获取安装在Windows XP计算机上的ODBC驱动程序列表?
我基本上希望看到(在.NET中)的内容:
控制面板 - >管理工具 - >数据源(ODBC) - >"驱动程序"选项卡.
Har*_*san 15
基本上系统在这里存储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;
}
Run Code Online (Sandbox Code Playgroud)
Jam*_*See 15
没有必要打开每个中间子项.读取注册表项以获取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;
}
Run Code Online (Sandbox Code Playgroud)
您还可以通过对SQLGetInstalledDriversW执行P/Invoke来实现该功能:
[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;
}
Run Code Online (Sandbox Code Playgroud)
我还调用该函数并使用如下结果在创建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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15302 次 |
| 最近记录: |