Use DataConnectionDialog to set the data source connection string at runtime.

Source: Internet
Author: User

Use DataConnectionDialog to set the data source connection string at runtime.

Introduction:

DataConnectionDialog class: Open the "Data Connection" dialog box to obtain the data connection information selected by the user.

The namespace is Microsoft. Data. ConnectionUI.

Assembly: Microsoft. Data. ConnectionUI. Dialog (in the "% Visual Studio installation directory %/Common7/IDE/Microsoft. Data. ConnectionUI. Dialog. dll ")

Note:

In the Microsoft. VisualStudio. Data (in Microsoft. VisualStudio. Data. dll) Microsoft. VisualStudio. Data namespace, there is also a DataConnectionDialog class, but this class is abstract. For example:

Operation:

First, you must reference Microsoft. Data. ConnectionUI. Dialog. dll (in the "% Visual Studio installation directory %/Common7/IDE/" directory ).

Then add the namespace:

using DataConnectionDialog = Microsoft.Data.ConnectionUI.DataConnectionDialog;using DataProvider = Microsoft.Data.ConnectionUI.DataProvider;using DataSource = Microsoft.Data.ConnectionUI.DataSource;

The key code is as follows:

Private string GetDatabaseConnectionString () {string result = string. empty; DataConnectionDialog dialog = new DataConnectionDialog (); dialog. dataSources. clear (); // Add a data source list. You can add at least one dialog to the window. dataSources. add (DataSource. accessDataSource); // Access dialog. dataSources. add (DataSource. sqlDataSource); // SQL Server dialog. dataSources. add (DataSource. oracleDataSource); // Oracle dialog. dataSources. add (DataSource. odbcDataSource); // Odbc dialog. dataSources. add (DataSource. sqlFileDataSource); // SQL Server File // sets the default data provider dialog. selectedDataSource = DataSource. sqlDataSource; dialog. selectedDataProvider = DataProvider. sqlDataProvider; // address of this article: http://www.cnblogs.com/Interkey/p/DataConnectionDialog.html // Dialog. title = "Cosmic_Spy"; // dialog. connectionString = "Data Source = *****; Initial Catalog = *****; Integrated Security = True "; // You can also set the default connection string. // you can only use the static method of the DataConnectionDialog class to Show the dialog box and cannot use the dialog class. show () or dialog. showDialog () to present the dialog box if (DataConnectionDialog. show (dialog) = DialogResult. OK) {result = dialog. connectionString;} return result ;}

Reference the above Code at the desired location to open the data source setting form at runtime, select the database (you can directly test whether the connection is available), and return the connection string!

Note:

To compare with the DataConnectionDialog class in Microsoft. VisualStudio. Data, part of the assembly code is pasted, such:

Summary:

This article only gives you a general understanding of the controls corresponding to DataConnectionDialog, so that you can quickly solve such requirements in future development.

Another important point in this article is: note thatAssemblyAndAssembly, module, and fileFor more information.

Refer:

 

Attachment: ConnectionPromptDialog.zip

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.