[ACCEPTED]-Listing ODBC Data Sources in C#-odbc
You could call the SQLDataSources-function 11 in ODBC32.DLL:
using System.Runtime.InteropServices;
public static class OdbcWrapper
{
[DllImport("odbc32.dll")]
public static extern int SQLDataSources(int EnvHandle, int Direction, StringBuilder ServerName, int ServerNameBufferLenIn,
ref int ServerNameBufferLenOut, StringBuilder Driver, int DriverBufferLenIn, ref int DriverBufferLenOut);
[DllImport("odbc32.dll")]
public static extern int SQLAllocEnv(ref int EnvHandle);
}
Example that lists the Data 10 Sources:
public void ListODBCsources()
{
int envHandle=0;
const int SQL_FETCH_NEXT = 1;
const int SQL_FETCH_FIRST_SYSTEM = 32;
if (OdbcWrapper.SQLAllocEnv(ref envHandle) != -1)
{
int ret;
StringBuilder serverName = new StringBuilder(1024);
StringBuilder driverName = new StringBuilder(1024);
int snLen = 0;
int driverLen = 0;
ret = OdbcWrapper.SQLDataSources(envHandle, SQL_FETCH_FIRST_SYSTEM, serverName, serverName.Capacity, ref snLen,
driverName, driverName.Capacity, ref driverLen);
while (ret == 0)
{
System.Windows.Forms.MessageBox.Show(serverName + System.Environment.NewLine + driverName);
ret = OdbcWrapper.SQLDataSources(envHandle, SQL_FETCH_NEXT, serverName, serverName.Capacity, ref snLen,
driverName, driverName.Capacity, ref driverLen);
}
}
}
The first call to SQLDataSources 9 with SQL_FETCH_FIRST_SYSTEM
tells the function to start the listing 8 with the System-DSNs. If you simply started 7 with SQL_FETCH_NEXT
it would first list the drivers. Link to the function ref on Microsofts site
Edit:
Everybody 6 seems to know it but I just found out yesterday 5 when I used this code in a new poject: if 4 you are compiling this with VS on a 64 Bit 3 Windows you have to set the "Target 2 Platform" to "x86" or the 1 code won't run.
I use the following code to retrieve the 4 DSNs from the registry :
private List<string> EnumDsn()
{
List<string> list = new List<string>();
list.AddRange(EnumDsn(Registry.CurrentUser));
list.AddRange(EnumDsn(Registry.LocalMachine));
return list;
}
private IEnumerable<string> EnumDsn(RegistryKey rootKey)
{
RegistryKey regKey = rootKey.OpenSubKey(@"Software\ODBC\ODBC.INI\ODBC Data Sources");
if (regKey != null)
{
foreach (string name in regKey.GetValueNames())
{
string value = regKey.GetValue(name, "").ToString();
yield return name;
}
}
}
It's strange that 3 you have non-english name for the "ODBC 2 Data Sources" key... I have a French version 1 of Windows, and the name is still in English
I don't think there is anything in .NET, and 4 a quick check of the (native) ODBC API shows 3 some functions that might be of help:
- SQLBrowseConnec
- SQLDrivers
Given 2 the way buffers are used in the ODBC API, careful 1 pinning of character arrays will be needed.
If you are using a Windows Forms Application 8 (not a Web environment), you could use the 7 Visual Studio's "Choose Data Source" dialog.
It's 6 included in an assembly and can be easily 5 used.
The article where I found this info: http://www.mztools.com/articles/2007/MZ2007011.aspx
In 4 any case, I'm from Spain and I also use 3 the Registry solution (specially in Web 2 apps). I've never found a machine with those 1 entries in a language different from English.
I know this is an old post but for what 4 it's worth at windows 10 I found the ODBC 3 connection located in the LocalMachine node 2 and not the current user, could be a 64bit 1 thing.
RegistryKey reg = (Registry.LocalMachine).OpenSubKey("Software");
reg = reg.OpenSubKey("ODBC");
reg = reg.OpenSubKey("ODBC.INI");
reg = reg.OpenSubKey("ODBC Data Sources");
string instance = "";
foreach (string item in reg.GetValueNames())
{
instance = item;
}
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.