com.joybase.DB.dll原始碼(4)
來源:互聯網
上載者:User
原始碼 /// <summary>
/// 內部保護的資料庫連接類
/// </summary>
protected abstract class Provider
{
//一個靜態串連介面;
private static System.Data.IDbConnection conn;
/// <summary>
/// 構造方法,設為私人類型,是防止外部無效的引用;
/// </summary>
protected Provider()
{
System.Data.IDbCommand x=new System.Data.OleDb.OleDbCommand();
}
/// <summary>
/// 返回資料庫邊接類型
/// </summary>
public static DBType Type
{
get
{
return Provider.m_DBType;
}
set
{
Provider.m_DBType=value;
}
}
private static string m_ConnectionString;
private static string m_ConnectionStringSetName;
/// <summary>
/// 資料庫連接字串設定名稱
/// </summary>
public static string ConnectionSetName
{
set
{
Provider.m_ConnectionStringSetName=value;
}
}
/// <summary>
/// 資料庫連接字串
/// </summary>
public static string ConnectionString
{
set
{
Provider.m_ConnectionString=value;
}
}
private static DBType m_DBType;
/// <summary>
/// 獲得資料庫連接介面
/// </summary>
/// <returns>返回一個串連</returns>
public static System.Data.IDbConnection getConn()
{
string ConnStr="";
try{
//System.Threading.Thread.GetDomain().UnhandledException+=new UnhandledExceptionEventHandler(ThrowDBException);
if(Provider.m_ConnectionString==""||Provider.m_ConnectionString==null)
{
if(Provider.m_ConnectionStringSetName.Trim()=="")
{
ConnStr=System.Configuration.ConfigurationSettings.AppSettings["DataBase.ConnectionString"];
}
else
{
ConnStr=System.Configuration.ConfigurationSettings.AppSettings[Provider.m_ConnectionStringSetName];
}
}
else
{
ConnStr=Provider.m_ConnectionString;
}
if(ConnStr==null||ConnStr=="")
{
throw new JoyBaseDBException("連接字串為空白或者是null類型,請檢查您的ConnectionString以及ConnectionSetName是否進行正確設定,或者閱讀相關說明.");
}
//DBType m_DBType;//=Provider.DataBaseType;
/*
* 注釋:我們對前面的編碼進行了部分的修改,鑒於System.Data.SqlClient的串連
* 字串當中不可能出現"Provider"字樣,所以我們根據是否有Provider字樣來判斷
* 該串連是基於System.Data.SqlClient的或者System.Data.OleDB的。
* 參考資料:
* 可以將 ConnectionString 屬性設定為單個單元。(不能為 SqlConnection 對象指定 Provider 屬性。)
* –或–
*
* 可以設定單個屬性(DataSource、Database、UserName 等等)。如果設定單個屬性,則將為您產生連接字串。
* 注意 在連接字串中儲存使用者名稱和密碼有安全性設定的意味。有關詳細資料,請參閱Introduction to ADO.NET Connection Design Tools。
*
*/
if(ConnStr.ToLower().IndexOf("provider")==-1) Provider.Type=DBType.SqlClient;
else Provider.Type=DBType.OleDB;
//throw new Exception("here");
if(m_DBType==DBType.SqlClient)
{
conn=new System.Data.SqlClient.SqlConnection(ConnStr);
}
else
{
conn=new System.Data.OleDb.OleDbConnection(ConnStr);
}
}
catch(Exception e)
{
string reason="(1)未設定資料庫連接字串,請重新檢查連接字串\r\n(2)目標資料庫不存在,或者是沒有啟動資料庫或者無法登入;\r\n(3)連接字串設定不正確,請按照標準的串連方式來寫。\r\n";
throw new JoyBaseDBException(e.Message,reason);
}
return conn;
}
}
}