get all ODBC drivers 驱动

get all ODBC drivers 驱动:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace CSConsoleTest2
{
    public static class Program
    {
        static void Main()
        {
            var drivers = GetSystemDriverList();

            string[] v = new string[4];
            v[0] = "server=127.0.0.1;database=TestData;uid=test;pwd=test";
            v[1] = "driver={SQL Server};server=sgpsql03.sgp.hp.com,2048;database=SPC_LOG;uid=logger;pwd=logger";
            v[2] = "Driver={Microsoft ODBC for Oracle};Server=FAB_DBS;Uid=fac_spc1;Pwd=12qwaszx;";
            v[3] = "server=.;database=TestData;uid=test;pwd=test";

            foreach (var item in v)
            {
                bool isOdbc = false;
                bool installed = false;
                string driver = GetODBCDriverString(item);
                if (!string.IsNullOrEmpty(driver))
                {
                    isOdbc = true;
                    if (drivers.Contains(driver))
                    {
                        installed = true;
                    }
                }
                else
                {
                    isOdbc = false;
                }
                if (isOdbc)
                {
                    Console.WriteLine("For String: " + item + Environment
                        .NewLine + "Is Ocbc: true, driver: " + driver + " , is installed: " + installed + Environment.NewLine);
                }
                else
                {
                    Console.WriteLine("For String: " + item + Environment
                        .NewLine + "Is Ocbc: false" + Environment.NewLine);
                }
            }


            Console.ReadLine();
        }

        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;
        }

        public static string GetODBCDriverString(string connectionStr)
        {
            var match = Regex.Match(connectionStr, @"(?<={).+(?=})");
            if (match != null && !string.IsNullOrEmpty(match.Value))
            {
                return match.Value;
            }
            return "";
        }
    }

}

  

原文地址:https://www.cnblogs.com/netact/p/3499495.html