com.joybase.DB.dll源代码(2)
2024-07-21 02:25:35
供稿:网友
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 [email protected]"即为一个合法的命令文本,我们可以以参数形式动态为@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语句或者存储过程使用不当,或者将特定数据库的检索语句使用到了不当的数据库上;/r/n(2)sql语句或者存储过程的入参的的赋值错误,如将字符串赋给了int类型等/r/n";
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变量或者将其设为不合理数值,而直接调用了分页方法/r/n;(2)检索的页号不存在或者溢出;/r/n";
throw new joybasedbexception(e.message,reason);
}
//this.m_parameters.clear();
return result;
}