Reputation: 3478
Is there any simple way to display a standard windows data sources dialog from a winforms application?
I'd like to show it to a user and pick up a system dsn or create a new one and return a datasource name. I haven't found any references to an existing wrappers in .net so I suppose I can only use a win API for that. Any existing solution or a snippet of code would be appreciated.
Upvotes: 3
Views: 2376
Reputation: 81
The standard ODBC selection dialog is a bit different than the one from the OP screenshot. The screenshot is the ODBC configuration dialog.
I think that the one the OP is after is the one that for example MS Access shows when linking/importing data which lacks many of the tabs from the screenshot. However it seems the standard for Windows ODBC selection as it is provided by the MS ODBC API.
In order to show it one has to call SQLDriverConnect providing a windows handle, an empty InConnectionString and the SQL_DRIVER_COMPLETE for the DriverCompletion parameter. Upon successful call the OutConnectionString parameter will be filled accordingly. If the dialog is cancelled by the user, then the result from the call will be SQL_NO_DATA.
PS. MS Access calls the function with a BufferLength parameter of 513 characters.
Upvotes: 0
Reputation: 3478
It seems that it is not possible to get the selected data source name from this dialog. Here is the winapi function which can be used to call this dialog (link):
BOOL SQLManageDataSources(HWND hwnd);
And here is a snippet:
[DllImport("ODBCCP32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern bool SQLManageDataSources(IntPtr hwnd);
private void ShowDataSourceDialog()
{
SQLManageDataSources(Handle);
}
Argument hwnd is a parent windows handle. Dialog is only displayed for a valid windows handle. Even though I can't select a data source this way, I can at least provide ability to add, change or remove data sources with an existing standard tool. Otherwise I need to create a custom one.
Upvotes: 2
Reputation: 12253
Since this is all data stored in the registry you could get a list of the ODBC connections available here:
HKEY_CURRENT_USER\Software\ODBC\ODBC.INI
Wrap this however you want to make it pretty.
There's some good info about querying the registry here
Upvotes: 0
Reputation: 4903
Maybe you could make a custom window for it where the user can insert/select a DSN.
There are some examples on how to manually insert new DSNs and list those already configured on the machine:
Check for System DSN and create System DSN if NOT Existing (iSeries Access ODBC Driver)
Dynamically adding DSN-names
ODBC Driver List from .NET
Upvotes: 0