使用静态工厂模式,通过传入枚举型参数,动态创建访问实例

实现模式上采用基本实现接口,派生类继承基类的虚函数,从而实现代码的耦合较低,有很好的扩展性。

spacer.gifpublicenum DBType
spacer.gifspacer.gif...{
spacer.gif        Access,
spacer.gif        SQL,
spacer.gif        DB2,
spacer.gif        Oracle,
spacer.gif        MySQL
spacer.gif    }
spacer.gif
spacer.gif
publicstaticclass DBAccessFactory
spacer.gifspacer.gif...{
spacer.gif
publicstatic IDBAccess Create(DBType type)
spacer.gifspacer.gif...{
spacer.gif            IDBAccess IRet =
null;
spacer.gif
switch (type)
spacer.gifspacer.gif...{
spacer.gif
case DBType.Access:
spacer.gif                    IRet =
new Access(type);
spacer.gif
break;
spacer.gif
spacer.gif
case DBType.SQL:
spacer.gif                    IRet =
new SQL(type);
spacer.gif
break;
spacer.gif
spacer.gif
default:
spacer.gif
break;
spacer.gif            }
spacer.gif
return IRet;
spacer.gif        }
spacer.gif
spacer.gif
privateabstractclass DBAccess : IDBAccess
spacer.gifspacer.gif...{
spacer.gif
protected DbConnection m_oConn = null;
spacer.gif
protectedconststring CON_strServer = "Server";
spacer.gif
protectedconststring CON_strDataBase = "Data Source";
spacer.gif
protectedconststring CON_strUser = "UID";
spacer.gif
protectedconststring CON_strPwd = "PWD";
spacer.gif
protectedconststring CON_strConnTimeOut = "Connect Timeout = 2";
spacer.gif
private DBType m_eDBType = DBType.Access;
spacer.gif
spacer.gif
protected DBAccess(DBType type)
spacer.gifspacer.gif...{
spacer.gif
this.m_eDBType = type;
spacer.gif            }
spacer.gif
spacer.gif
public DBType DBType
spacer.gifspacer.gif...{
spacer.gifspacer.gif
get...{ returnthis.m_eDBType; }
spacer.gif            }
spacer.gif
spacer.gif
publicvoid Init(string strServer, string strDataBase, string strUser, string strPwd)
spacer.gifspacer.gif...{
spacer.gif
this.InitConn(strServer, strDataBase, strUser, strPwd);
spacer.gif            }
spacer.gif
spacer.gif
publicvoid Open()
spacer.gifspacer.gif...{
spacer.gif
if (this.m_oConn != null)
spacer.gifspacer.gif...{
spacer.gif
this.m_oConn.Open();
spacer.gif                }
spacer.gif            }
spacer.gif
spacer.gif
publicint RunNoQuery(string strCmd)
spacer.gifspacer.gif...{
spacer.gif
int iRet = 0;
spacer.gif
try
spacer.gifspacer.gif...{
spacer.gif                    DbCommand oCmd =
this.GetCmd(strCmd);
spacer.gif
if (oCmd != null)
spacer.gifspacer.gif...{
spacer.gif                        iRet = oCmd.ExecuteNonQuery();
spacer.gif                    }
spacer.gif                }
spacer.gif
catch (Exception ex)
spacer.gifspacer.gif...{
spacer.gif
throw (new Exception(ex.Message));
spacer.gif                }
spacer.gif
return iRet;
spacer.gif            }
spacer.gif
spacer.gif
publicint GetFiledMax(string strTable, string strField)
spacer.gifspacer.gif...{
spacer.gif
int iRet = -1;
spacer.gif                DataTable dt =
this.RunQuery("Select Max(" + strField + ") From " + strTable);
spacer.gif
if (dt != null && dt.Rows.Count == 1)
spacer.gifspacer.gif...{
spacer.gif                    iRet = dt.Rows[0][0]
is DBNull ? 0 : Convert.ToInt32(dt.Rows[0][0]);
spacer.gif                }
spacer.gif
return iRet;
spacer.gif            }
spacer.gif
spacer.gif
public DataTable RunQuery(string strCmd)
spacer.gifspacer.gif...{
spacer.gif                DataTable dt =
new DataTable();
spacer.gif                DbDataAdapter adp =
this.DbAdp;
spacer.gif                adp.SelectCommand =
this.GetCmd(strCmd);
spacer.gif                adp.Fill(dt);
spacer.gif
return dt;
spacer.gif            }
spacer.gif
spacer.gif
publicvoid Close()
spacer.gifspacer.gif...{
spacer.gif
if (this.m_oConn != null && this.m_oConn.State == System.Data.ConnectionState.Open)
spacer.gifspacer.gif...{
spacer.gif
this.m_oConn.Close();
spacer.gif                }
spacer.gif            }
spacer.gif
spacer.gif
publicbool TestConn()
spacer.gifspacer.gif...{
spacer.gif
bool bRet = true;
spacer.gif
try
spacer.gifspacer.gif...{
spacer.gif
if (this.m_oConn.State != System.Data.ConnectionState.Open)
spacer.gifspacer.gif...{
spacer.gif
this.m_oConn.Open();
spacer.gif                    }
spacer.gif                    bRet =
this.m_oConn.State == System.Data.ConnectionState.Open;
spacer.gif                }
spacer.gif
catch
spacer.gifspacer.gif...{
spacer.gif                    bRet =
false;
spacer.gif                }
spacer.gif
this.Close();
spacer.gif
return bRet;
spacer.gif            }
spacer.gif
spacer.gifspacer.gif
publicabstract DataTable Tables ...{ get; }
spacer.gif
publicabstract DataTable GetColumns();
spacer.gif
publicabstract DataTable GetColumns(string strTable);
spacer.gif
spacer.gif
protectedabstractvoid InitConn(string strServer, string strDataBase, string strUser, string strPwd);
spacer.gif
protectedabstract DbCommand GetCmd(string strCmd);
spacer.gifspacer.gif
protectedabstract DbDataAdapter DbAdp ...{ get;}
spacer.gif        }
spacer.gif   }