我有一个应用程序从计算机中提取所有ODBC连接并将它们放在一个组合框中。然后,用户从组合框中选择ODBC连接,并提供用户名和密码,然后用于连接到数据库。我已经能够以这种方式连接到一个简单的Northwind Access数据库,但是当针对更复杂的数据库进行测试时,我收到的错误是驱动程序错误。这就是我用来获取ODBC连接的内容:
RegistryKey sysODBC = (Registry.LocalMachine).OpenSubKey("SOFTWARE").OpenSubKey("ODBC").OpenSubKey("ODBC.INI").OpenSubKey("ODBC Data Sources");
string[] DSNArray = sysODBC.GetValueNames().ToArray();
OdbcConnection conn = new System.Data.Odbc.OdbcConnection(ConnectionString);
我看到有一个
conn.Driver
属性,但我很难找到如何加载驱动程序信息以及如何将其分配给驱动程序属性所需的语法。任何帮助都将不胜感激。
答案 0 :(得分:0)
代码可以清理一下,但这对我有用:
public List<Tuple<string, string>> ListODBCsources()
{
int envHandle = 0;
const int SQL_FETCH_NEXT = 1;
const int SQL_FETCH_FIRST_SYSTEM = 32;
List<Tuple<string, string>> ODBCNameDriverList = new List<Tuple<string, string>>();
if (OdbcWrapper.SQLAllocEnv(ref envHandle) != -1)
{
int returnValue;
StringBuilder serverName = new StringBuilder(1024);
StringBuilder driverName = new StringBuilder(1024);
int snLen = 0;
int driverLen = 0;
returnValue = OdbcWrapper.SQLDataSources(envHandle, SQL_FETCH_FIRST_SYSTEM, serverName, serverName.Capacity, ref snLen,
driverName, driverName.Capacity, ref driverLen);
while (returnValue == 0)
{
ODBCNameDriverList.Add(Tuple.Create(serverName.ToString(), driverName.ToString()));
//MessageBox.Show(serverName + System.Environment.NewLine + driverName);
returnValue = OdbcWrapper.SQLDataSources(envHandle, SQL_FETCH_NEXT, serverName, serverName.Capacity, ref snLen,
driverName, driverName.Capacity, ref driverLen);
}
}
return ODBCNameDriverList;
}