c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(二)
使用靜態(tài)工廠模式,通過傳入枚舉型參數(shù),動態(tài)創(chuàng)建訪問實例
實現(xiàn)模式上采用基本實現(xiàn)接口,派生類繼承基類的虛函數(shù),從而實現(xiàn)代碼的耦合較低,有很好的擴展性。
publicenum DBType
...{
? ? ? ?Access,
? ? ? ?SQL,
? ? ? ?DB2,
? ? ? ?Oracle,
? ? ? ?MySQL
? ?}
publicstaticclass DBAccessFactory
...{
publicstatic IDBAccess Create(DBType type)
...{
? ? ? ? ? ?IDBAccess IRet = null;
switch (type)
...{
case DBType.Access:
? ? ? ? ? ? ? ? ? ?IRet = new Access(type);
break;
case DBType.SQL:
? ? ? ? ? ? ? ? ? ?IRet = new SQL(type);
break;
default:
break;
? ? ? ? ? ?}
return IRet;
? ? ? ?}
privateabstractclass DBAccess : IDBAccess
...{
protected DbConnection m_oConn = null;
protectedconststring CON_strServer = "Server";
protectedconststring CON_strDataBase = "Data Source";
protectedconststring CON_strUser = "UID";
protectedconststring CON_strPwd = "PWD";
protectedconststring CON_strConnTimeOut = "Connect Timeout = 2";
private DBType m_eDBType = DBType.Access;
protected DBAccess(DBType type)
...{
this.m_eDBType = type;
? ? ? ? ? ?}
public DBType DBType
...{
get...{ returnthis.m_eDBType; }
? ? ? ? ? ?}
publicvoid Init(string strServer, string strDataBase, string strUser, string strPwd)
...{
this.InitConn(strServer, strDataBase, strUser, strPwd);
? ? ? ? ? ?}
publicvoid Open()
...{
if (this.m_oConn != null)
...{
this.m_oConn.Open();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
publicint RunNoQuery(string strCmd)
...{
int iRet = 0;
try
...{
? ? ? ? ? ? ? ? ? ?DbCommand oCmd = this.GetCmd(strCmd);
if (oCmd != null)
...{
? ? ? ? ? ? ? ? ? ? ? ?iRet = oCmd.ExecuteNonQuery();
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?}
catch (Exception ex)
...{
throw (new Exception(ex.Message));
? ? ? ? ? ? ? ?}
return iRet;
? ? ? ? ? ?}
publicint GetFiledMax(string strTable, string strField)
...{
int iRet = -1;
? ? ? ? ? ? ? ?DataTable dt = this.RunQuery("Select Max(" + strField + ") From " + strTable);
if (dt != null && dt.Rows.Count == 1)
...{
? ? ? ? ? ? ? ? ? ?iRet = dt.Rows[0][0] is DBNull ? 0 : Convert.ToInt32(dt.Rows[0][0]);
? ? ? ? ? ? ? ?}
return iRet;
? ? ? ? ? ?}
public DataTable RunQuery(string strCmd)
...{
? ? ? ? ? ? ? ?DataTable dt = new DataTable();
? ? ? ? ? ? ? ?DbDataAdapter adp = this.DbAdp;
? ? ? ? ? ? ? ?adp.SelectCommand = this.GetCmd(strCmd);
? ? ? ? ? ? ? ?adp.Fill(dt);
return dt;
? ? ? ? ? ?}
publicvoid Close()
...{
if (this.m_oConn != null && this.m_oConn.State == System.Data.ConnectionState.Open)
...{
this.m_oConn.Close();
? ? ? ? ? ? ? ?}
? ? ? ? ? ?}
publicbool TestConn()
...{
bool bRet = true;
try
...{
if (this.m_oConn.State != System.Data.ConnectionState.Open)
...{
this.m_oConn.Open();
? ? ? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ? ? ?bRet = this.m_oConn.State == System.Data.ConnectionState.Open;
? ? ? ? ? ? ? ?}
catch
...{
? ? ? ? ? ? ? ? ? ?bRet = false;
? ? ? ? ? ? ? ?}
this.Close();
return bRet;
? ? ? ? ? ?}
publicabstract DataTable Tables ...{ get; }
publicabstract DataTable GetColumns();
publicabstract DataTable GetColumns(string strTable);
protectedabstractvoid InitConn(string strServer, string strDataBase, string strUser, string strPwd);
protectedabstract DbCommand GetCmd(string strCmd);
protectedabstract DbDataAdapter DbAdp ...{ get;}
? ? ? ?}
? }
轉載于:https://blog.51cto.com/ciscoxia/1216323
總結
以上是生活随笔為你收集整理的c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电源不稳定
- 下一篇: windows Service循环任务.