Database Connection Dialog

Implementation of a database connection dialog with full set of features which you can see when look at the same dialog in  is not the easiest tasks. It includes many aspects such as servers enumeration and database enumeration. Visual Studio SDK allows you to create such a dialog in several minutes.

I’m talking about well-known standard dialog which Visual Studio and some other applications have. It looks like this:

First of all, you need to add DLL library Microsoft.Data.ConnectionUI.Dialog.dll  to references of your project. It could be found in C:\Program Files\Microsoft Visual Studio 8\Common7\IDE  if your version of VS was installed with default settings.
This is the simplest code to launch the dialog:

DataConnectionDialog connectionDialog = new DataConnectionDialog();
DataSource.AddStandardDataSources(connectionDialog);
if (DataConnectionDialog.Show(connectionDialog) == DialogResult.OK)
{
    Settings.Default.DatabaseConnection = connectionDialog.ConnectionString;
}

If you run this code you’ll see a dialog asking you to choose a data source to go ahead to the next step.

For some reason we might already know which data source we want use for a connection. In such a case we can specify it exactly before launch the dialog. See the following code:

DataConnectionDialog connectionDialog = new DataConnectionDialog();
DataSource.AddStandardDataSources(connectionDialog);
DataSource _sqlDefaultSource = null;
DataProvider _sqlDefaultProvider = null;
List toDeleteSrc = new List();
List toDeletePrv = new List();

// remove all data source except that one we need
toDeleteSrc.AddRange(connectionDialog.DataSources);
foreach (DataSource source in connectionDialog.DataSources)
{
    if (source.Name == "MicrosoftSqlServer") // SQL Server data source
    {
        _sqlDefaultSource = source;
        toDeleteSrc.Remove(source);

        // remove all data providers except that one we need
        toDeletePrv.AddRange(source.Providers);
        foreach (DataProvider provider in source.Providers)
        {
            if (provider.ShortDisplayName == "SqlClient") // Sql Native Client data provider
            {
                _sqlDefaultProvider = provider;
                toDeletePrv.Remove(provider);
                break;
            }
        }
        break;
    }
}

// check if the target data source/provider are found
if ((_sqlDefaultProvider == null) || (_sqlDefaultSource == null))
    throw new NullReferenceException("There is no SQL server provider.");

// remove unnecessary data sources
foreach (DataSource source in toDeleteSrc)
    connectionDialog.DataSources.Remove(source);

// remove unnecessary data providers
foreach (DataProvider provider in toDeletePrv)
    _sqlDefaultSource.Providers.Remove(provider);

// specify data source and provider
connectionDialog.SelectedDataSource = _sqlDefaultSource;
connectionDialog.SelectedDataProvider = _sqlDefaultProvider;

// specify previously selected connection
connectionDialog.ConnectionString = Settings.Default.DatabaseConnection;
if (DataConnectionDialog.Show(connectionDialog) == DialogResult.OK)
{
    Settings.Default.DatabaseConnection = connectionDialog.ConnectionString;
}

If you run this code you’ll see the dialog box which is present on the first picture for this topic.

Tags: ,

3 thoughts on “Database Connection Dialog”

  1. Emmad Kareem says:

    While I have not tried this code yet, it is a great idea. Thanks for sharing

    1. Thanks. Just keep in mind that it’s 10 years old. I’m not sure if this technique is still valid.

  2. Tms says:

    Where The Database Connection Dialog for sqlLite, Mysql ? How we add them to this dialog

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Post

%d bloggers like this: