get all ODBC drivers 驱动
get all ODBC drivers 驱动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 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 "" ; } } } |
posted on 2013-12-31 15:15 Henry_Wang 阅读(365) 评论(0) 编辑 收藏 举报
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步