资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(一)收藏首先定义数据库操作的标准接口IDBAccess,定义接口的基本功能;通过基本的接口设置,完成数据访问的统一抽象。publicinterfaceIDBAccess.voidInit(stringstrServer,stringstrDataBase,stringstrUser,stringstrPwd);voidOpen();voidClose();boolTestConn();intRunNoQuery(stringstrCmd);DataTableRunQuery(stringstrCmd);DBTypeDBType.get;intGetFiledMax(stringstrTable,stringstrField);DataTableTables.get;DataTableGetColumns();DataTableGetColumns(stringstrTable);c#下各种数据库操作的封装!(支持ACCESS,SQLSERVER,DB2,ORACLE,MYSQL)(二)收藏使用静态工厂模式,通过传入枚举型参数,动态创建访问实例实现模式上采用基本实现接口,派生类继承基类的虚函数,从而实现代码的耦合较低,有很好的扩展性。publicenumDBType.Access,SQL,DB2,Oracle,MySQLpublicstaticclassDBAccessFactory.publicstaticIDBAccessCreate(DBTypetype).IDBAccessIRet=null;switch(type).caseDBType.Access:IRet=newAccess(type);break;caseDBType.SQL:IRet=newSQL(type);break;default:break;returnIRet;privateabstractclassDBAccess:IDBAccess.protectedDbConnectionm_oConn=null;protectedconststringCON_strServer=Server;protectedconststringCON_strDataBase=DataSource;protectedconststringCON_strUser=UID;protectedconststringCON_strPwd=PWD;protectedconststringCON_strConnTimeOut=ConnectTimeout=2;privateDBTypem_eDBType=DBType.Access;protectedDBAccess(DBTypetype).this.m_eDBType=type;publicDBTypeDBType.get.returnthis.m_eDBType;publicvoidInit(stringstrServer,stringstrDataBase,stringstrUser,stringstrPwd).this.InitConn(strServer,strDataBase,strUser,strPwd);publicvoidOpen().if(this.m_oConn!=null).this.m_oConn.Open();publicintRunNoQuery(stringstrCmd).intiRet=0;try.DbCommandoCmd=this.GetCmd(strCmd);if(oCmd!=null).iRet=oCmd.ExecuteNonQuery();catch(Exceptionex).throw(newException(ex.Message);returniRet;publicintGetFiledMax(stringstrTable,stringstrField).intiRet=-1;DataTabledt=this.RunQuery(SelectMax(+strField+)From+strTable);if(dt!=null&dt.Rows.Count=1).iRet=dt.Rows00isDBNull?0:Convert.ToInt32(dt.Rows00);returniRet;publicDataTableRunQuery(stringstrCmd).DataTabledt=newDataTable();DbDataAdapteradp=this.DbAdp;adp.SelectCommand=this.GetCmd(strCmd);adp.Fill(dt);returndt;publicvoidClose().if(this.m_oConn!=null&this.m_oConn.State=System.Data.ConnectionState.Open).this.m_oConn.Close();publicboolTestConn().boolbRet=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();returnbRet;publicabstractDataTableTables.get;publicabstractDataTableGetColumns();publicabstractDataTableGetColumns(stringstrTable);protectedabstractvoidInitConn(stringstrServer,stringstrDataBase,stringstrUser,stringstrPwd);protectedabstr
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号