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