Data | database | database connection | data Source | string
We often provide an interface between the optional data source and the data connection configuration in the development application to facilitate the user to configure the database connection string. A typical approach is to write this information in a 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 error, 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 Microsoft.Data.ConnectionUI.Dialog.dll, which is under 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:
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 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 show dialog box only through static method of Dataconnectiondialog class
Different use of dialog. Show () or dialog. ShowDialog () To render the dialog box
if (dataconnectiondialog.show (dialog, this) = = DialogResult.OK)
{
Txtconnectionstring.text = dialog. ConnectionString;
}
}
}
}