直接调用内置数据源连接对话框

来源:百度文库 编辑:神马文学网 时间:2024/05/24 03:14:32
'先引用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);

这样就可以随便改数据库连接了,呵呵,效果不错