Skip to content
Advertisement

Display a ConnectionString dialog

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement