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