繁体中文
设为首页
加入收藏
当前位置:.Net技术首页 >> 数据库应用 >> com.joybase.DB.dll源代码(2)

com.joybase.DB.dll源代码(2)

2004-10-01 08:26:10  作者:  来源:互联网  浏览次数:7  文字大小:【】【】【
简介:  public class Command {   //private DBParameters m_Parameters;   /// <summary>   /// 数据库命令,System.Data.IDbCommand接口类型   /// </sum...
关键字:源代码 joybase dll com DB

public class Command

{

//private DBParameters m_Parameters;

///

/// 数据库命令,System.Data.IDbCommand接口类型

///

private System.Data.IDbCommand m_command;

///

/// 内部参数,每页的纪录数

///

private int m_PageSize;

///

/// 总共的页数

///

private int m_PageCount;

///

/// 总共的纪录数

///

private int m_RecordCount;

///

/// SQL语句及存储过程的命令文本,字符串类型

///

private string m_CommandText;

///

/// 命令参数集合

///

private System.Collections.Hashtable m_Parameter;

///

/// 只写属性,将连接字符串在Config文件中的键名赋值进来

///

[Category("(Data Binding)"),Description("数据库连接字符串在Config文件中的键名")]

public string ConnectionSetName

{

set

{

Provider.ConnectionSetName=value;

}

}

///

/// 只读属性,返回总共的页数

///

[Browsable(false)]

public int PageCount

{

get

{

return this.m_PageCount;

}

}

///

/// 只写属性,设置每页的纪录数

///

[Category("Pager Info"),Description("每页显示的纪录数")]

public int PageSize

{

set

{

this.m_PageSize=value;

}

}

///

/// 只读属性,获得总共的纪录数

///

[Browsable(false)]

public int RecordCount

{

get

{

return this.m_RecordCount;

}

}

///

/// 构造方法

///

public Command()

{

this.m_PageCount=0;

this.m_PageSize=0;

this.m_RecordCount=0;

m_CommandText="";

m_Parameter=new System.Collections.Hashtable();

}

///

/// 只写属性,连接字符串,注意,本属性可以覆盖ConnectionSetName属性的值

///

[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;

}

}

///

/// 字符串类型,SQL语句及存储过程的命令文本,只读

///

///

/// SQL语句可以以入参方式表示,如"select * from user where username=@username"即为一个合法的命令文本,我们可以以参数形式动态为@username赋值

///

[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;

}

}

///

/// 设置当前的参数类型。

///

[Category("(Data Binding)"),Description("设置连接字符串")]

public System.Collections.Hashtable Parameter

{

set

{

this.m_Parameter=value;

}

get

{

return this.m_Parameter;

}

}

// ///

// /// 暂不支持

// ///

// public DBParameters Parameters

// {

// get

// {

// return this.m_Parameters;

//

//

// }

// set

// {

// this.m_Parameters=value;

// }

// }

//

///

/// 得到出口参数的值,仅在命令文本为存储过程且设置了出口参数时有效;

///

public System.Data.IDataParameterCollection ReturnValue

{

get

{

return this.m_command.Parameters;

}

}

///

/// 内部处理方法,不对外公开

///

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语句或者存储过程使用不当,或者将特定数据库的检索语句使用到了不当的数据库上;\r\n(2)SQL语句或者存储过程的入参的的赋值错误,如将字符串赋给了int类型等\r\n";

throw new JoyBaseDBException(e.Message,reason);

}

}

///

/// 执行一次更新或者插入操作

///

/// 返回该次操作所影响的纪录集数

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;

}

///

/// 执行查询操作,并且按照规定的分页形式返回DataReader结果集

///

/// 需要返回的页面号

/// 返回一个System.Data.IDataReader方法

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

{

result.Read();

}

}

catch(Exception e)

{

string reason="(1)未设置PageSize变量或者将其设为不合理数值,而直接调用了分页方法\r\n;(2)检索的页号不存在或者溢出;\r\n";

throw new JoyBaseDBException(e.Message,reason);

}

//this.m_Parameters.Clear();

return result;

}

责任编辑:admin
相关文章