开发者

ODBC Driver List from .NET

Is there a way to get a list of ODBC drivers that are installed on a Windows XP machine from .NET?

I basically would like to see (in .NET) what is in:

Control Panel->Administrative Tools->Data Sou开发者_如何学JAVArces (ODBC)->"Drivers" Tab.


It is not necessary to open every intermediate subkey. Reading the registry key to get the ODBC driver names can be done in a much 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 also implement the function by doing a P/Invoke 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 function and use the results as follows to gracefully degrade to prior versions of the SQL driver when creating ODBC data 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;
    }


See this or this

Basically system stores the ODBC Driver's information here HKEY_LOCAL_MACHINE\SOFTWARE\ODBC\ODBCINST.INI\ODBC Drivers

You can use this or similar code to find out the installed ODBC Drivers. This code basically reads 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;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜