直接调用内置数据源连接对话框,C#/VB.NET2005源码

'先引用Microsoft.Data.ConnectionUI.Dialog.dll(在VS2005安装路径的IDE目录下)

'将以下代码加入相应事件过程

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

谢谢 Tonyyang 转为C#,在此一并归纳,但此C#代码我没有测试过,请发现问题的同行指正:

C#2005 Code

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;

DataConnectionDialog.Show(dialog);

if (dialog.DialogResult == DialogResult.OK)

{ textBox1.Text = dialog.ConnectionString; }

else if (dialog.DialogResult == DialogResult.Cancel)

{ }

//Me.Close();