一个数据库操作类的实例(一.Command类部分)
2024-07-21 02:23:46
供稿:网友
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");
}
}
}
}