[ACCEPTED]-ODBC Driver List from .NET-vb.net
It is not necessary to open every intermediate 9 subkey. Reading the registry key to get 8 the ODBC driver names can be done in a much 7 more compact fashion as follows:
/// <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;
}
You can 6 also implement the function by doing a P/Invoke 5 to 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;
}
I also call the 4 function and use the results as follows 3 to gracefully degrade to prior versions 2 of the SQL driver when creating ODBC data 1 sources:
/// <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;
}
Basically system stores the ODBC 4 Driver's information here
HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers
You can use this 3 or similar code to find out the installed 2 ODBC Drivers. This code basically reads 1 the drivers info from registry
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;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.