C# 通用数据库配置界面,微软原生DLL重整合
微软vs自带数据库测试界面化工具
Microsoft.Data.ConnectionUI.DataConnection.Dialog 与Microsoft.Data.ConnectionUI.DataConnection
MicrosoftVisualStudio里面的资源之数据库连接配置
这个功能的实现主要是用了Microsoft.Data.ConnectionUI.dll和Microsoft.Data.ConnectionUI.Dialog.dll这个二个动态库,可以在VS的安装目录的Common7IDE这个目录下面找到(本机的安装路径C:Program Files (x86)Microsoft Visual Studio 14.0Common7IDE)。
找到两个组件 Microsoft.Data.ConnectionUI.dll,Microsoft.Data.ConnectionUI.Dialog.dll
这两个要一起引用下
Microsoft.Data.ConnectionUI.dll
Microsoft.Data.ConnectionUI.Dialog.dll
测试代码。ok,但是本地的只引用Microsoft.Data.ConnectionUI.dll,Microsoft.Data.ConnectionUI.Dialog.dll没有引用Microsoft.Data.ConnectionUI.Dialog.resources.dll,界面是英文版的,使用DBConnectSetting.dll,就可以是中文界面了(因为DBConnectSetting.dll整合了上述的三个dll类库)。
string ConnString = null;
bool isSqlServer = false;
private void btnTestDataConnectionDialog_Click(object sender, EventArgs e)
{
//服务器名称 LENOVO-PCMYSQLEXPRESS
using (Microsoft.Data.ConnectionUI.DataConnectionDialog dlg = new DataConnectionDialog())
{
//添加数据源
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.AccessDataSource);
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.OdbcDataSource);
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.OracleDataSource);
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource);
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlFileDataSource);
//设置默认数据提供对象
dlg.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.SqlDataSource;
dlg.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.SqlDataProvider;
dlg.Text = "连接DCWriter演示数据库";
//只能够通过DataConnectionDialog类的静态方法Show出对话框
//不同使用dialog.Show()或dialog.ShowDialog()来呈现对话框
if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dlg) == System.Windows.Forms.DialogResult.OK)
{
//连接字符串"Data Source=LENOVO-PC\MYSQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True"
ConnString = dlg.ConnectionString;
isSqlServer = dlg.SelectedDataSource == Microsoft.Data.ConnectionUI.DataSource.SqlDataSource
|| dlg.SelectedDataSource == Microsoft.Data.ConnectionUI.DataSource.SqlFileDataSource;
}
MessageBox.Show(ConnString);
}
}
这是本人写的代码的一部分,后面会展示具体的界面显示
private static IDbConnection InnerCreateConnect()
{
if (string.IsNullOrEmpty(_ConnectionString))
{
string dbFile = System.IO.Path.Combine(Application.StartupPath, "EMR.mdb");
if (File.Exists(dbFile))
{
// 存在演示数据库文件
_ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=EMR.mdb";
_IsSQLServer = false;
}
else
{
try
{
using (Microsoft.Data.ConnectionUI.DataConnectionDialog dlg = new Microsoft.Data.ConnectionUI.DataConnectionDialog())
{
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.AccessDataSource); // Access
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlDataSource); // Sql Server
dlg.DataSources.Add(Microsoft.Data.ConnectionUI.DataSource.SqlFileDataSource); // Sql Server File
// 初始化
dlg.SelectedDataSource = Microsoft.Data.ConnectionUI.DataSource.AccessDataSource;
dlg.SelectedDataProvider = Microsoft.Data.ConnectionUI.DataProvider.OleDBDataProvider;
dlg.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=www.mdb";
dlg.Text = "连接演示数据库";
//只能够通过DataConnectionDialog类的静态方琺Show出对话框
//不同使用dialog.Show()或dialog.ShowDialog()来呈现对话框
if (Microsoft.Data.ConnectionUI.DataConnectionDialog.Show(dlg) == DialogResult.OK)
{
_ConnectionString = dlg.ConnectionString;
_IsSQLServer = dlg.SelectedDataSource == Microsoft.Data.ConnectionUI.DataSource.SqlDataSource
|| dlg.SelectedDataSource == Microsoft.Data.ConnectionUI.DataSource.SqlFileDataSource;
}
}
}
catch (Exception ext)
{
MessageBox.Show(ext.Message);
_ConnectionString = null;
}
}
}
if (string.IsNullOrEmpty(_ConnectionString) == false)
{
if (_IsSQLServer)
{
SqlConnection conn = new SqlConnection(_ConnectionString);
return conn;
}
else
{
OleDbConnection conn = new System.Data.OleDb.OleDbConnection(_ConnectionString);
return conn;
}
}
return null;
}