Display a ConnectionString dialog

26,167

Solution 1

Note: The dialog component referred to below is no longer available for download. Unless you have retrieved it in the past, you will probably not get this answer's sample code to work.

Alternative: There is now a different DataConnectionDialog available on NuGet. See this answer for details.


"Data Connection Dialog" on MSDN Archive Gallery (broken as of 1 Sept. 2015)

The data connection dialog is a database tool component released with Visual Studio. It allows users to build connection strings and to connect to specific data sources. try this..

C# Sample:

static void Main(string[] args)
{
    DataConnectionDialog dcd = new DataConnectionDialog();
    DataConnectionConfiguration dcs = new DataConnectionConfiguration(null);
    dcs.LoadConfiguration(dcd);

    if (DataConnectionDialog.Show(dcd) == DialogResult.OK)
    {
        // load tables
        using (SqlConnection connection = new SqlConnection(dcd.ConnectionString))
        {
            connection.Open();
            SqlCommand cmd = new SqlCommand("SELECT * FROM sys.Tables", connection);
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    Console.WriteLine(reader.HasRows);
                }
            }
        }
    }
    dcs.SaveConfiguration(dcd);
}

Here source code also available. we can integrate and redistribute the source code with our application according to license.

enter image description here

Solution 2

The data connection dialog component linked to in this answer is no longer available for download.

However, a (apparently somewhat altered) DataConnectionDialog component has since become available on NuGet.

Installation:

Add the component to your Visual Studio project via the NuGet package manager console:

Install-Package DataConnectionDialog

Usage example:

// using Microsoft.Data.ConnectionUI;
// using System.Windows.Forms;

bool TryGetDataConnectionStringFromUser(out string outConnectionString)
{
    using (var dialog = new DataConnectionDialog())
    {
        // If you want the user to select from any of the available data sources, do this:
        DataSource.AddStandardDataSources(dialog);

        // OR, if you want only certain data sources to be available
        // (e.g. only SQL Server), do something like this instead: 
        dialog.DataSources.Add(DataSource.SqlDataSource);
        dialog.DataSources.Add(DataSource.SqlFileDataSource);
        …

        // The way how you show the dialog is somewhat unorthodox; `dialog.ShowDialog()`
        // would throw a `NotSupportedException`. Do it this way instead:
        DialogResult userChoice = DataConnectionDialog.Show(dialog);

        // Return the resulting connection string if a connection was selected:
        if (userChoice == DialogResult.OK)
        { 
            outConnectionString = dialog.ConnectionString;
            return true;
        }
        else
        {
            outConnectionString = null;
            return false;
        }
    }
}

Solution 3

I think all the other answers here are out-of-date, but I found a current solution at code.msdn.microsoft.com:

Using Microsoft Visual Studio Connection Dialog at runtime

In Visual Studio when a developer wants to create strong typed classes for database tables either for the conventional TableAdapter or Entity Framework there is a place in the process where a dialog is displayed as shown below. I will show you how to do this at runtime and a bit more.

The download is a solution that builds the following dlls:

  • Microsoft.Data.ConnectionUI.Dialog.dll

  • Microsoft.Data.ConnectionUI.dll

  • Microsoft.Data.DataConnectionConfiguration.dll

The solution also contains a sample application showing how to use them. Worked a treat for me and it is super easy.

Solution 4

Yes and no.

Yes, it is technically possible, but I urge you not to; that dialog is part of Visual Studio and is lot listed in "redist". My interpretation is that you are not free to redistribute this dll.

Share:
26,167

Related videos on Youtube

Christian Tang
Author by

Christian Tang

http://seaweed.dk http://christiantang.dk http://willitcompile.net

Updated on July 09, 2022

Comments

  • Christian Tang
    Christian Tang almost 2 years

    I'm trying to create a program in C# that should be able to create, backup and restore a SQL Server database.

    For this, the user needs to be able to setup a connection string to the desired SQL Server (and database).

    I would like to use the same dialog as for example Visual Studio for creating the connection string.

    Is this possible?

  • Ranhiru Jude Cooray
    Ranhiru Jude Cooray almost 13 years
    According to this link connect.microsoft.com/VisualStudio/feedback/details/291885/… and blogs.msdn.com/b/vsdata/archive/2010/02/02/…, the source code for this component was released.
  • Christian Tang
    Christian Tang over 12 years
    This looks promising. The DataConnectionDialog wasn't hard to find. But I can't seem to find the DataConnectionConfiguration class. Which namespace/assembly is this class located in?
  • David Sherret
    David Sherret almost 12 years
    For anyone else who couldn't find DataConnectionConfiguration... it's in the Sample project.
  • nekno
    nekno over 10 years
    And if you don't want to save your configuration to a file, you can skip the DataConnectionConfiguration class from the sample project and use a couple built-in calls from the project to load up the dialog: DataConnectionDialog dcd = new DataConnectionDialog(); Microsoft.Data.ConnectionUI.DataSource.AddStandardDataSource‌​s(dcd); dcd.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.SqlDataSource; dcd.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider; DataConnectionDialog.Show(dcd);
  • Oscar
    Oscar almost 9 years
    Just for the record, it's now a Nuget package: nuget.org/packages/DataConnectionDialog
  • Jonathan Allen
    Jonathan Allen over 6 years
    DataConnectionDialog is no longer listed on NuGet.
  • Jonathan Allen
    Jonathan Allen over 6 years
    DataConnectionDialog is no longer listed on NuGet
  • user2091150
    user2091150 over 4 years
    Also broken link.
  • NoChance
    NoChance over 4 years
    It would be nice if you could tell us about the correct link. Thanks.
  • NoChance
    NoChance over 4 years
    @stakx - It is not misleading now that your answer points to Nuget package that is not longer there.
  • Andrew
    Andrew almost 4 years
  • DuroForce
    DuroForce almost 4 years
    Is there a way to use Active Directory as authentication type with this component?
  • shA.t
    shA.t over 3 years
    Nuget Note: The owner has unlisted this package. This could mean that the package is deprecated or shouldn't be used anymore.