Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
146 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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;
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...