日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(二)

發布時間:2025/1/21 数据库 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.


使用靜態工廠模式,通過傳入枚舉型參數,動態創建訪問實例

實現模式上采用基本實現接口,派生類繼承基類的虛函數,從而實現代碼的耦合較低,有很好的擴展性。

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)(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。