日期:2014-05-20  浏览次数:20787 次

如何调用设置数据库连接字符串的对话框
我想在运行期弹出设置数据库连接字符串的对话框,就想.net设计期编辑连接字符串弹出的一样。

------解决方案--------------------
新建一个文本文件,把拓展名改成.udl,双击运行即可
------解决方案--------------------
先引用Microsoft.Data.ConnectionUI.Dialog.dll(在VS2005安装路径的IDE目录下)
例如:D:\Program Files\Microsoft Visual Studio 8\Common7\IDE
VB:
Dim dialog As DataConnectionDialog = New DataConnectionDialog()
dialog.DataSources.Add(DataSource.SqlDataSource)
dialog.DataSources.Add(DataSource.OdbcDataSource)
dialog.DataSources.Add(DataSource.OracleDataSource)
dialog.DataSources.Add(DataSource.AccessDataSource)

dialog.SelectedDataSource = DataSource.SqlDataSource
dialog.SelectedDataProvider = DataProvider.SqlDataProvider

DataConnectionDialog.Show(dialog)
If dialog.DialogResult = Windows.Forms.DialogResult.OK Then
Me.TextBox1.Text = dialog.ConnectionString
ElseIf dialog.DialogResult = Windows.Forms.DialogResult.Cancel Then
Me.Close()
End If

C# 2005
DataConnectionDialog dialog = new DataConnectionDialog();
dialog.DataSources.Add(DataSource.SqlDataSource);
dialog.DataSources.Add(DataSource.OdbcDataSource);
dialog.DataSources.Add(DataSource.OracleDataSource);
dialog.DataSources.Add(DataSource.AccessDataSource);

dialog.SelectedDataSource = DataSource.SqlDataSource;
dialog.SelectedDataProvider = DataProvider.SqlDataProvider;

string strCon = " ";
DataConnectionDialog.Show(dialog);
if (dialog.DialogResult == DialogResult.OK)
{ strCon = dialog.ConnectionString; }
else if (dialog.DialogResult == DialogResult.Cancel)
{ }

MessageBox.Show(strCon);

直接可以运行的代码