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