以前做Winform程序,居然专门做一个界面来配置连接字符串。今天无意中发现:竟然可以直接调用VS.net2005中的配置界面来处理。

使用方法也非常简单:

引用C:\Program Files\Microsoft Visual Studio 8\Common7\IDE 里面的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);

            }



 ​