Microsoft.Data.ConnectionUI.DataConnectionDialog
Posted LongtengGensSupreme
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Microsoft.Data.ConnectionUI.DataConnectionDialog相关的知识,希望对你有一定的参考价值。
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的主要内容,如果未能解决你的问题,请参考以下文章