using System; namespace com.joybase.DB { /// <summary> /// 数据库操作类,通过这一个类的对外接口,可以获得以下特性: /// 1.不必区分数据库类型,而去考虑是使用System.Data.SqlClient实现,或者使用System.Data.OleDB来实现; /// 2.可以将SQL语句进行模式化,比如输入“select * from tablename where username=? order by ? desc",然后再用建模语句将该语句保持完整; /// 3.数据库连接实现Connection Pool(该特性是建立在ADO.NET的基础上的) /// 4.支持SQL语句的DataReader输出、无输出或者DataSet输出; /// 5.支持ADO.NET事务级处理; /// 6.支持存储过程的输入及输出; /// </summary> public class Command { //最终的SQL语句; private System.Text.StringBuilder m_SQL; //中间过程的SQL语句数组 private string[] m_SQLArr; //替代字符串的数组; private string[] m_ReplaceText; //替代字符串的个数; private int m_Count; //连接类型; private System.Data.CommandType m_CommandType; //在配置文件中用来配置数据库连接字符串的标签; private string m_ConnstringSetName; /// <summary> /// 构造方法 /// </summary> /// <param name="p_sql">模式SQL语句</param> public Command(string p_sql) { if(p_sql==null) throw new Exception("Error Value"); if(p_sql.Trim()=="") throw new Exception("Error Value"); this.m_SQL=new System.Text.StringBuilder(); m_SQLArr=p_sql.Split('?'); m_Count=m_SQLArr.Length; m_ReplaceText=new string[m_Count-1]; m_ConnstringSetName=""; this.m_CommandType=System.Data.CommandType.Text; } /// <summary> /// 在配置文件中用来配置数据库连接字符串的标签,如果不设置,则将采用“DataBase.ConnectionString”作为连接字符串使用 /// </summary> public string ConnStringSetName { set { this.m_ConnstringSetName=value; } } /// <summary> /// 所执行命令的类型,即是存储过程还是普通SQL语句; /// </summary> public System.Data.CommandType CommandType { set { this.m_CommandType=value; } } /// <summary> /// 以指定的字符串替代在p_loaction位置上的“?”; /// </summary> /// <param name="p_location">位置索引</param> /// <param name="p_InString">欲替代的字符串</param> public void setString(int p_location,string p_InString) { if(p_InString==null) throw new Exception("Error Value"); if(p_InString.Trim()==null) throw new Exception("Error Value"); m_ReplaceText[p_location-1]="'"+p_InString.Trim()+"'"; } /// <summary> /// 以指定的整数类型,替代在p_location位置上的"?" /// </summary> /// <param name="p_location">位置索引</param> /// <param name="p_InInt">欲替代的整数</param> public void setInt(int p_location,int p_InInt) { m_ReplaceText[p_location-1]=p_InInt.ToString(); } /// <summary> /// 以指定的长整数类型,替代在p_location位置上的"?" /// </summary> /// <param name="p_location">位置索引</param> /// <param name="p_InLong">欲替代的长整数</param> public void setLong(int p_location,int p_InLong) { m_ReplaceText[p_location-1]=p_InLong.ToString(); } /// <summary> /// 以指定的时间,替代在p_location位置上的"?" /// </summary> /// <param name="p_location">位置索引</param> /// <param name="p_InDateTime">欲替代的时间</param> public void setDateTime(int p_location,System.DateTime p_InDateTime) { m_ReplaceText[p_location-1]=p_InDateTime.ToString(); } /// <summary> /// 以系统时间替代在p_location位置的"?" /// </summary> /// <param name="p_location">位置索引</param> /// <param name="p_OnLyDate">如果为True,则仅替代到日期,如果为False,则替代到时间</param> public void setDateTime(int p_location,bool p_OnlyDate) { if(p_OnlyDate) m_ReplaceText[p_location-1]=System.DateTime.Now.Date.ToString(); else m_ReplaceText[p_location-1]=System.DateTime.Now.ToString(); }
private string JoinSQLString() { for(int i=0;i<m_Count;i++) { if(i!=0) this.m_SQL.Append(m_ReplaceText[i-1]); this.m_SQL.Append(m_SQLArr[i]);
} return this.m_SQL.ToString(); } /// <summary> /// 执行命令并且返回System.Data.IDataReader结果的结果集 /// </summary> /// <returns></returns> public System.Data.IDataReader Result() { System.Data.IDataReader result=null; try { System.Data.IDbCommand command=Provider.getConn(m_ConnstringSetName).CreateCommand(); command.CommandText=this.JoinSQLString(); command.CommandType=this.m_CommandType; command.Connection.Close(); command.Connection.Open(); command.Prepare(); //if(Provider.getConn(m_ConnstringSetName).State==System.Data.ConnectionState.Closed) //Provider.getConn(m_ConnstringSetName).Open(); result=command.ExecuteReader(); //command.Connection.Close();
//Provider.getConn().Close(); } catch { throw new Exception("error at execute db command"); } return result; } /// <summary> /// 无结果执行命令 /// </summary> public void ExecuteNoResult() { try { System.Data.IDbCommand command=Provider.getConn(m_ConnstringSetName).CreateCommand(); command.CommandText=this.JoinSQLString(); command.CommandType=this.m_CommandType; command.Connection.Close(); command.Connection.Open(); //if(Provider.getConn(m_ConnstringSetName).State==System.Data.ConnectionState.Closed) //Provider.getConn(m_ConnstringSetName).Open(); command.ExecuteNonQuery();
} catch { throw new Exception("Error at execute DB Command"); }
} } }
|