标签:
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=EMR.mdb";
dlg.Text = "连接DCWriter演示数据库";
//只能够通过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;
}
Microsoft.Data.ConnectionUI.DataConnectionDialog
标签:
原文地址:http://www.cnblogs.com/1175429393wljblog/p/5553409.html