Configure database connection in the data source selection dialog box in VS [Figure]

Source: Internet
Author: User

In actual development, we often provide interfaces for data source selection and data connection configuration in applications to facilitate user configuration of database connection strings. A typical method is to write the information in the XML configuration file, such as the web. config or app. config file.

However, the customer is not a professional programmer. Accidentally writing a connection string error may cause the system to fail. It is best to allow the user to select the data source and data connection information through a graphical interface, for example:

You can call the database connection in Visual Studio to configure the UI.

The specific implementation method is as follows:

First, add. data. connectionUI. dialog. dll reference. This assembly is under the installation directory of VS2005, C: \ Program Files \ Microsoft Visual Studio 8 \ Common7 \ IDE, and my VS is installed on drive C. The following code is used:

Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Text;
Using System. Windows. Forms;
Using Microsoft. Data. ConnectionUI;
Namespace ConnectionBuilder
{
Public partial class ConnectionForm: Form
{
Public ConnectionForm ()
{
InitializeComponent ();
}

Private void btnCreateConnection (object sender, EventArgs e)
{
DataConnectionDialog dialog = new DataConnectionDialog ();

// Add a data source list. You can add the data source types required by your program to the window.
Dialog. CES. Add (DataSource. SqlDataSource );
Dialog. CES. Add (DataSource. OdbcDataSource );

Dialog. SelectedDataSource = DataSource. OdbcDataSource;
Dialog. SelectedDataProvider = DataProvider. OdbcDataProvider;

// You can only Show the dialog box through the static method of the DataConnectionDialog class.
// Use dialog. Show () or dialog. ShowDialog () to present the dialog box.
If (DataConnectionDialog. Show (dialog, this) = DialogResult. OK)
{
TxtConnectionString. Text = dialog. ConnectionString;
}
}
}
}

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.