Search code examples
c#windowswinformsdialogodbc

How to show a standard windows data sources (ODBC) dialog


Is there any simple way to display a standard windows data sources dialog from a winforms application?

enter image description here

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.


Solution

  • 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.