Simple implementation of database connection configuration through the Data Source Selection dialog box in vs [Figure]_c# Tutorial

Source: Internet
Author: User

In actual development , we often provide an interface for selecting the data source and data connection configuration in the application to facilitate the user to configure the database connection string. A typical approach is to write this information in an XML configuration file , such as a web.config or app.config file .

But the customer is not a professional programmer , accidentally will make the connection string write wrong , resulting in the system can not run, the best is to enable users to select a graphical interface to the data source and data connection information , The following figure:

We can simply implement this by invoking The database connection configuration UI in Visual Studio .

The specific implementation methods are as follows:

First, add a reference to the Microsoft.Data.ConnectionUI.Dialog.dll , which is assembly in the VS2005 installation directory,C:\Program Files\Microsoft Visual Studio 8\Common7\IDE below, my VS is installed in C disk, here is the code:

UsingSystem;
UsingSystem.Collections.Generic;
UsingSystem.ComponentModel;
UsingSystem.Data;
UsingSystem.Drawing;
UsingSystem.Text;
UsingSystem.Windows.Forms;
UsingMicrosoft.Data.ConnectionUI;
NamespaceConnectionbuilder
{
PublicPartialClassConnectionform:form
{
PublicConnectionform ()
{
InitializeComponent ();
}

PrivatevoidBtncreateconnection (Objectsender, EventArgs e)
{
Dataconnectiondialog Dialog=NewDataconnectiondialog ();

//Add a list of data sources to add to the window the type of data source you need for your program
Dialog. Datasources.add (Datasource.sqldatasource);
Dialog. Datasources.add (Datasource.odbcdatasource);

Dialog. Selecteddatasource=Datasource.odbcdatasource;
Dialog. Selecteddataprovider=Dataprovider.odbcdataprovider;

// can only show the dialog box through the static method of the Dataconnectiondialog class
            &NBSP // uses dialog differently. Show () or dialog. ShowDialog () To render 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.