Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display a ConnectionString dialog

Tags:

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?

like image 392
Christian Tang Avatar asked Aug 01 '11 07:08

Christian Tang


People also ask

How do I set ConnectionString?

In the Properties window, expand the Connection node. To quickly modify the connection string, edit the ConnectionString property, or click the down arrow on the Connection property and choose New Connection.

How do I find my ConnectionString?

Right-click on your connection and select "Properties". You will get the Properties window for your connection. Find the "Connection String" property and select the "connection string". So now your connection string is in your hands; you can use it anywhere you want.

Which is the ConnectionString property?

The ConnectionString property can be set only when the connection is closed. Many of the connection string values have corresponding read-only properties. When the connection string is set, these properties are updated, except when an error is detected. In this case, none of the properties are updated.

What is ConnectionString in C#?

Connection String is a normal String representation which contains Database connection information to establish the connection between Database and the Application.


1 Answers

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;         }     } } 
like image 134
stakx - no longer contributing Avatar answered Sep 19 '22 15:09

stakx - no longer contributing