您的位置:首页 > 其它

get all ODBC drivers 驱动

2013-12-31 15:15 246 查看
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 "";
}
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: