问题
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.
回答1:
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.
回答2:
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
回答3:
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
来源:https://stackoverflow.com/questions/14816607/how-to-show-a-standard-windows-data-sources-odbc-dialog