com.joybase.DB.dll源代码(2)
点击次数:16 次 发布日期:2008-11-27 00:43:37 作者:源代码网
|
public class Command { //private DBParameters m_Parameters; /// <summary> /// 数据库命令,System.Data.IDbCommand接口类型 /// </summary> private System.Data.IDbCommand m_command; /// <summary> /// 内部参数,每页的纪录数 /// </summary> private int m_PageSize; /// <summary> /// 总共的页数 /// </summary> private int m_PageCount; /// <summary> /// 总共的纪录数 /// </summary> private int m_RecordCount; /// <summary> /// SQL语句及存储过程的命令文本,字符串类型 /// </summary> private string m_CommandText; /// <summary> /// 命令参数集合 /// </summary> private System.Collections.Hashtable m_Parameter; /// <summary> /// 只写属性,将连接字符串在Config文件中的键名赋值进来 /// </summary> [Category("(Data Binding)"),Description("数据库连接字符串在Config文件中的键名")] public string ConnectionSetName { set { Provider.ConnectionSetName=value; } } /// <summary> /// 只读属性,返回总共的页数 /// </summary> [Browsable(false)] public int PageCount { get { return this.m_PageCount; } } /// <summary> /// 只写属性,设置每页的纪录数 /// </summary> [Category("Pager Info"),Description("每页显示的纪录数")] public int PageSize { set { this.m_PageSize=value; } } /// <summary> /// 只读属性,获得总共的纪录数 /// </summary> [Browsable(false)] public int RecordCount { get { return this.m_RecordCount; } } /// <summary> /// 构造方法 /// </summary> public Command() { this.m_PageCount=0; this.m_PageSize=0; this.m_RecordCount=0; m_CommandText=""; m_Parameter=new System.Collections.Hashtable(); } /// <summary> /// 只写属性,连接字符串,注意,本属性可以覆盖ConnectionSetName属性的值 /// </summary> [Browsable(true),Category("(Data Binding)"),Description("设置数据库连接字符串"),Editor(typeof(FolderNameEditor), typeof(UITypeEditor))] public string test { get { System.Resources.ResourceManager rm=new System.Resources.ResourceManager(typeof(Command)); return rm.GetString("hello"); } } public string ConnectionString { set { Provider.ConnectionString=value; } } /// <summary> /// 字符串类型,SQL语句及存储过程的命令文本,只读 /// </summary> /// <remarks> /// SQL语句可以以入参方式表示,如"select * from user where username=@username"即为一个合法的命令文本,我们可以以参数形式动态为@username赋值 /// </remarks> [Category("(DataBinding"),Description("需要执行的SQL查询的文本以及存储过程的名称")] public string CommandText { set { m_command=Provider.getConn().CreateCommand(); //this.m_Parameters=(DBParameters)this.m_command.Parameters; this.m_CommandText=value; } } /// <summary> /// 设置当前的参数类型。 /// </summary> [Category("(Data Binding)"),Description("设置连接字符串")] public System.Collections.Hashtable Parameter { set { this.m_Parameter=value; } get { return this.m_Parameter; } } // /// <summary> // /// 暂不支持 // /// </summary> // public DBParameters Parameters // { // get // { // return this.m_Parameters; // // // } // set // { // this.m_Parameters=value; // } // } // /// <summary> /// 得到出口参数的值,仅在命令文本为存储过程且设置了出口参数时有效; /// </summary> public System.Data.IDataParameterCollection ReturnValue { get { return this.m_command.Parameters; } } /// <summary> /// 内部处理方法,不对外公开 /// </summary> private void Prepare() { //System.Threading.Thread.GetDomain().UnhandledException+=new UnhandledExceptionEventHandler(ThrowDBException); try { //m_command=Provider.getConn().CreateCommand(); m_command.CommandText=this.m_CommandText; System.Collections.IDictionaryEnumerator One=this.m_Parameter.GetEnumerator(); while(One.MoveNext()) { System.Data.IDataParameter parameter=this.m_command.CreateParameter(); parameter.SourceVersion =System.Data.DataRowVersion.Current; parameter.Value=One.Value; parameter.ParameterName=(string)One.Key; this.m_command.Parameters.Add(parameter); } this.m_command.Connection.Close(); this.m_command.Connection.Open(); //this.m_command.Prepare(); } catch(Exception e) { string reason="(1)SQL语句或者存储过程使用不当,或者将特定数据库的检索语句使用到了不当的数据库上; (2)SQL语句或者存储过程的入参的的赋值错误,如将字符串赋给了int类型等 "; throw new JoyBaseDBException(e.Message,reason); } } /// <summary> /// 执行一次更新或者插入操作 /// </summary> /// <returns>返回该次操作所影响的纪录集数</returns> public int ExecuteNoResult() { this.Prepare(); int result=0; try { result=this.m_command.ExecuteNonQuery(); } catch(Exception e) { throw new JoyBaseDBException(e.Message,e); } //this.m_Parameters.Clear(); return result; } /// <summary> /// 执行查询操作,并且按照规定的分页形式返回DataReader结果集 /// </summary> /// <param name="p_CurrentPageIndex">需要返回的页面号</param> /// <returns>返回一个System.Data.IDataReader方法</returns> public System.Data.IDataReader ExecuteDataReader(int p_CurrentPageIndex) { System.Data.IDataReader result=null; this.Prepare(); try { result=this.m_command.ExecuteReader(); System.Data.DataSet ds=this.ExecuteDataSet(); this.m_RecordCount=ds.Tables[0].Rows.Count; // ds.Clear(); ds.Dispose(); if(this.m_RecordCount%this.m_PageSize==0) { this.m_PageCount=this.m_RecordCount/this.m_PageSize; } else { this.m_PageCount=this.m_RecordCount/this.m_PageSize+1; } int StartCount=(p_CurrentPageIndex-1)*this.m_PageSize; for(int i=0;i<StartCount;i++) { result.Read(); } } catch(Exception e) { string reason="(1)未设置PageSize变量或者将其设为不合理数值,而直接调用了分页方法 ;(2)检索的页号不存在或者溢出; "; throw new JoyBaseDBException(e.Message,reason); } //this.m_Parameters.Clear(); return result; } 源代码网供稿. |
