首先定义数据库操作的标准接口IDBAccess,定义接口的基本功能;
通过基本的接口设置,完成数据访问的统一抽象。
public interface IDBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
void Init(string strServer, string strDataBase, string strUser, string strPwd);
void Open();
void Close();
bool TestConn();
int RunNoQuery(string strCmd);
DataTable RunQuery(string strCmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
DBType DBType ...{ get;}
int GetFiledMax(string strTable, string strField);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
DataTable Tables ...{ get; }
DataTable GetColumns();
DataTable GetColumns(string strTable);
}
使用静态工厂模式,通过传入枚举型参数 ,动态创建访问实例
实现模式上采用基本实现接口,派生类继承基类的虚函数,从而实现代码的耦合较低,有很好的扩展性。
public enum DBType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
Access,
SQL,
DB2,
Oracle,
MySQL
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static class DBAccessFactory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public static IDBAccess Create(DBType type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
IDBAccess IRet = null;
switch (type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case DBType.Access:
IRet = new Access(type);
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DBType.SQL:
IRet = new SQL(type);
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
default:
break;
}
return IRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private abstract class DBAccess : IDBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
protected DbConnection m_oConn = null;
protected const string CON_strServer = "Server";
protected const string CON_strDataBase = "Data Source";
protected const string CON_strUser = "UID";
protected const string CON_strPwd = "PWD";
protected const string CON_strConnTimeOut = "Connect Timeout = 2";
private DBType m_eDBType = DBType.Access;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected DBAccess(DBType type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_eDBType = type;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public DBType DBType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return this.m_eDBType; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Init(string strServer, string strDataBase, string strUser, string strPwd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.InitConn(strServer, strDataBase, strUser, strPwd);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Open()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (this.m_oConn != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_oConn.Open();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int RunNoQuery(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int iRet = 0;
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DbCommand oCmd = this.GetCmd(strCmd);
if (oCmd != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
iRet = oCmd.ExecuteNonQuery();
}
}
catch (Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
throw (new Exception(ex.Message));
}
return iRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int GetFiledMax(string strTable, string strField)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int iRet = -1;
DataTable dt = this.RunQuery("Select Max(" + strField + ") From " + strTable);
if (dt != null && dt.Rows.Count == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
iRet = dt.Rows[0][0] is DBNull ? 0 : Convert.ToInt32(dt.Rows[0][0]);
}
return iRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public DataTable RunQuery(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DataTable dt = new DataTable();
DbDataAdapter adp = this.DbAdp;
adp.SelectCommand = this.GetCmd(strCmd);
adp.Fill(dt);
return dt;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Close()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (this.m_oConn != null && this.m_oConn.State == System.Data.ConnectionState.Open)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_oConn.Close();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool TestConn()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
bool bRet = true;
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (this.m_oConn.State != System.Data.ConnectionState.Open)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_oConn.Open();
}
bRet = this.m_oConn.State == System.Data.ConnectionState.Open;
}
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
bRet = false;
}
this.Close();
return bRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public abstract DataTable Tables ...{ get; }
public abstract DataTable GetColumns();
public abstract DataTable GetColumns(string strTable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected abstract void InitConn(string strServer, string strDataBase, string strUser, string strPwd);
protected abstract DbCommand GetCmd(string strCmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
protected abstract DbDataAdapter DbAdp ...{ get;}
}
}
各派生类的具体实现,此时只要实现DBAccess的各个虚函数即可。
private class Access : DBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public Access(DBType type)
: base(type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void InitConn(string strServer, string strDataBase, string strUser, string strPwd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string strConn = "Provider = ";
switch (strDataBase.Substring(strDataBase.LastIndexOf('.') + 1).ToLower())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case "mdb": // 2000, 2003
strConn += "Microsoft.Jet.OleDb.4.0;";
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case "accdb": // 2007
strConn += "Microsoft.ACE.OLEDB.12.0;";
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
default:
throw (new Exception("Unknown Access Version."));
//break;
}
strConn += CON_strDataBase + " = " + strDataBase;
//strConn += CON_strUser + " = " + strUser;
//strConn += CON_strPwd + " = " + strPwd;
base.m_oConn = new OleDbConnection(strConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbCommand GetCmd(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return new OleDbCommand(strCmd, (OleDbConnection)base.m_oConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbDataAdapter DbAdp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return new OleDbDataAdapter(); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable Tables
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
return ((OleDbConnection)base.m_oConn).GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] ...{ null, null, null, "Table" });
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DataTable dt = new DataTable();
foreach (DataRow row in this.Tables.Rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
dt.Merge(this.GetColumns(row["TABLE_NAME"].ToString()));
}
return dt;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns(string strTable)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
return ((OleDbConnection)base.m_oConn).GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] ...{ null, null, strTable, null });
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
private class SQL : DBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public SQL(DBType type)
: base(type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void InitConn(string strServer, string strDataBase, string strUser, string strPwd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string strConn = CON_strServer + " = " + strServer + ";";
strConn += CON_strDataBase + " = " + strDataBase + ";";
strConn += CON_strUser + " = " + strUser + ";";
strConn += CON_strPwd + " = " + strPwd + ";";
strConn += CON_strConnTimeOut;
base.m_oConn = new SqlConnection(strConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbCommand GetCmd(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return new SqlCommand(strCmd, (SqlConnection)base.m_oConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbDataAdapter DbAdp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return new SqlDataAdapter(); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable Tables
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return ((SqlConnection)base.m_oConn).GetSchema("Tables", null); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return ((SqlConnection)base.m_oConn).GetSchema("Columns", null);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns(string strTable)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
return ((SqlConnection)base.m_oConn).GetSchema("Columns", new string[] ...{ null, null, strTable, null });
}
}
整个例子的完整代码如下,VS2008下编译测试通过,本人已通过它实现N个项目,欢迎大家评测,谢谢指教
public enum DBType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
Access,
SQL,
DB2,
Oracle,
MySQL
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public interface IDBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
void Init(string strServer, string strDataBase, string strUser, string strPwd);
void Open();
void Close();
bool TestConn();
int RunNoQuery(string strCmd);
DataTable RunQuery(string strCmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
DBType DBType ...{ get;}
int GetFiledMax(string strTable, string strField);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
DataTable Tables ...{ get; }
DataTable GetColumns();
DataTable GetColumns(string strTable);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public static class DBAccessFactory
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
...{
public static IDBAccess Create(DBType type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
IDBAccess IRet = null;
switch (type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case DBType.Access:
IRet = new Access(type);
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case DBType.SQL:
IRet = new SQL(type);
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
default:
break;
}
return IRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private abstract class DBAccess : IDBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
protected DbConnection m_oConn = null;
protected const string CON_strServer = "Server";
protected const string CON_strDataBase = "Data Source";
protected const string CON_strUser = "UID";
protected const string CON_strPwd = "PWD";
protected const string CON_strConnTimeOut = "Connect Timeout = 2";
private DBType m_eDBType = DBType.Access;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected DBAccess(DBType type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_eDBType = type;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public DBType DBType
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return this.m_eDBType; }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Init(string strServer, string strDataBase, string strUser, string strPwd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.InitConn(strServer, strDataBase, strUser, strPwd);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Open()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (this.m_oConn != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_oConn.Open();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int RunNoQuery(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int iRet = 0;
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DbCommand oCmd = this.GetCmd(strCmd);
if (oCmd != null)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
iRet = oCmd.ExecuteNonQuery();
}
}
catch (Exception ex)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
throw (new Exception(ex.Message));
}
return iRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public int GetFiledMax(string strTable, string strField)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
int iRet = -1;
DataTable dt = this.RunQuery("Select Max(" + strField + ") From " + strTable);
if (dt != null && dt.Rows.Count == 1)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
iRet = dt.Rows[0][0] is DBNull ? 0 : Convert.ToInt32(dt.Rows[0][0]);
}
return iRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public DataTable RunQuery(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DataTable dt = new DataTable();
DbDataAdapter adp = this.DbAdp;
adp.SelectCommand = this.GetCmd(strCmd);
adp.Fill(dt);
return dt;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Close()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (this.m_oConn != null && this.m_oConn.State == System.Data.ConnectionState.Open)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_oConn.Close();
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public bool TestConn()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
bool bRet = true;
try
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
if (this.m_oConn.State != System.Data.ConnectionState.Open)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
this.m_oConn.Open();
}
bRet = this.m_oConn.State == System.Data.ConnectionState.Open;
}
catch
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
bRet = false;
}
this.Close();
return bRet;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public abstract DataTable Tables ...{ get; }
public abstract DataTable GetColumns();
public abstract DataTable GetColumns(string strTable);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected abstract void InitConn(string strServer, string strDataBase, string strUser, string strPwd);
protected abstract DbCommand GetCmd(string strCmd);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
protected abstract DbDataAdapter DbAdp ...{ get;}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
Access, SQL#region Access, SQL
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private class Access : DBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
public Access(DBType type)
: base(type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void InitConn(string strServer, string strDataBase, string strUser, string strPwd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string strConn = "Provider = ";
switch (strDataBase.Substring(strDataBase.LastIndexOf('.') + 1).ToLower())
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
case "mdb": // 2000, 2003
strConn += "Microsoft.Jet.OleDb.4.0;";
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
case "accdb": // 2007
strConn += "Microsoft.ACE.OLEDB.12.0;";
break;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
default:
throw (new Exception("Unknown Access Version."));
//break;
}
strConn += CON_strDataBase + " = " + strDataBase;
//strConn += CON_strUser + " = " + strUser;
//strConn += CON_strPwd + " = " + strPwd;
base.m_oConn = new OleDbConnection(strConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbCommand GetCmd(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return new OleDbCommand(strCmd, (OleDbConnection)base.m_oConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbDataAdapter DbAdp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return new OleDbDataAdapter(); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable Tables
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
get
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
return ((OleDbConnection)base.m_oConn).GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] ...{ null, null, null, "Table" });
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
DataTable dt = new DataTable();
foreach (DataRow row in this.Tables.Rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
dt.Merge(this.GetColumns(row["TABLE_NAME"].ToString()));
}
return dt;
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns(string strTable)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
return ((OleDbConnection)base.m_oConn).GetOleDbSchemaTable(OleDbSchemaGuid.Columns, new object[] ...{ null, null, strTable, null });
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private class SQL : DBAccess
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
public SQL(DBType type)
: base(type)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override void InitConn(string strServer, string strDataBase, string strUser, string strPwd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
string strConn = CON_strServer + " = " + strServer + ";";
strConn += CON_strDataBase + " = " + strDataBase + ";";
strConn += CON_strUser + " = " + strUser + ";";
strConn += CON_strPwd + " = " + strPwd + ";";
strConn += CON_strConnTimeOut;
base.m_oConn = new SqlConnection(strConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbCommand GetCmd(string strCmd)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return new SqlCommand(strCmd, (SqlConnection)base.m_oConn);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected override DbDataAdapter DbAdp
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return new SqlDataAdapter(); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable Tables
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
get ...{ return ((SqlConnection)base.m_oConn).GetSchema("Tables", null); }
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns()
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
return ((SqlConnection)base.m_oConn).GetSchema("Columns", null);
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public override DataTable GetColumns(string strTable)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
return ((SqlConnection)base.m_oConn).GetSchema("Columns", new string[] ...{ null, null, strTable, null });
}
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
#endregion
}