1、在VS(此处目录文件以vs2010为例)安装目录下(“%Visual Studio安装目录%/Common10/IDE/Microsoft.Data.ConnectionUI.Dialog.dll”)找到Microsoft.Data.ConnectionUI.dll、Microsoft.Data.ConnectionUI.Dialog.dll和汉化资源文件Microsoft.Data.ConnectionUI.Dialog.resources.dll,注意汉化资源文件需要放在zh-CHS文件夹下;
2、在工程文件中添加对以上二个dll文件的引用;
3、具体代码如下:
private void button1_Click(object sender, EventArgs e) { DataConnectionDialog dialog = new DataConnectionDialog(); dialog.DataSources.Add(DataSource.AccessDataSource); dialog.DataSources.Add(DataSource.OdbcDataSource); dialog.DataSources.Add(DataSource.OracleDataSource); dialog.DataSources.Add(DataSource.SqlDataSource); dialog.DataSources.Add(DataSource.SqlFileDataSource); dialog.SelectedDataSource = DataSource.SqlDataSource; dialog.SelectedDataProvider = DataProvider.SqlDataProvider; if (DataConnectionDialog.Show(dialog, this) == DialogResult.OK) { //connDlg.ConnectionString; } }运行效果:
重新打开时展示,之前选择的连接字符串:
DataConnectionDialog dialog = new DataConnectionDialog(); dialog.DataSources.Add(DataSource.AccessDataSource); dialog.DataSources.Add(DataSource.OdbcDataSource); dialog.DataSources.Add(DataSource.OracleDataSource); dialog.DataSources.Add(DataSource.SqlDataSource); dialog.DataSources.Add(DataSource.SqlFileDataSource); dialog.SelectedDataSource = DataSource.SqlDataSource; dialog.SelectedDataProvider = DataProvider.SqlDataProvider; //之前 DataConnectionDialog 控件保存的连接字符串 dialog.ConnectionString = str; //ConnectionString有值,则会显示出来 DataConnectionDialog.Show(dialog, this);