How to show the Windows Standard Data Sources (ODBC) Dialog Box

Is there an easy way to display the standard Windows data sources dialog from a winforms application?

enter image description here

I would like to show it to the user and pick up the dsn system, or create a new one and return the datasource name. I have not found any references to existing wrappers in .net, so I guess I can use the win API for this. Any existing solution or piece of code would be appreciated.

+3


source to share


3 answers


It seems impossible to get the selected datasource name from this dialog. Here is the winapi function that can be used to invoke this dialog ( link ):

BOOL SQLManageDataSources(HWND hwnd);

      

And here's a snippet:



[DllImport("ODBCCP32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SQLManageDataSources(IntPtr hwnd);

private void ShowDataSourceDialog()
{
    SQLManageDataSources(Handle);
}

      

The hwnd argument is the parent window handle. The dialog is displayed only for a valid window handle. While I cannot select a data source this way, I can at least provide the ability to add, modify, or delete data sources using the existing standard tool. Otherwise, I need to create a custom.

+1


source


Maybe you can create your own window where user can insert / select DSN.
There are several examples on how to manually insert new DSNs and list the ones that are already configured on the machine:



Check system DSN and create system DSN if NOT Existing (ODBC driver for iSeries Access)
Add DSNs dynamically
List ODBC drivers from .NET

0


source


Since this is all data stored in the registry, you can get a list of available ODBC connections:

HKEY_CURRENT_USER\Software\ODBC\ODBC.INI

      

Wrap it up, but you want to make it pretty.

There's good information about querying the registry here

0


source







All Articles