以前做Winform程序,居然專門做一個界面來配置連接字符串。今天無意中發現:竟然可以直接調用VS.net2005中的配置界面來處理。
使用方法也非常簡單:
引用C:Program FilesMicrosoft Visual Studio 8Common7IDE 裡面的Microsoft.Data.ConnectionUI.Dialog.dll。
using Microsoft.Data.ConnectionUI;
DataConnectionDialog dialog = new DataConnectionDialog();
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.SelectedDataProvider = DataProvider.SqlDataProvider;
//dialog.DataSources.Add(DataSource.OdbcDataSource);
//dialog.SelectedDataSource = DataSource.OdbcDataSource;
//dialog.SelectedDataProvider = DataProvider.OdbcDataProvider;
dialog.StartPosition = FormStartPosition.CenterScreen;
dialog.Title = "Connect to SqlServer";
dialog.ConnectionString = "server=127.0.0.1;database=YarnNew;user id=sa;password=aaa;";
if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK)
{
MessageBox.Show(dialog.ConnectionString);
}