#region using
namespace wis.base.data
{
/// <summary>
/// <table >
/// <tr><td><b>文 件 名</b>:dbobject.cs</td></tr>
/// <tr><td><b>功能描述</b>:数据层基类,提供对底层数据的基本操作</td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 [email protected] qq:23106676</td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table>
/// </summary>
public class dbobject
{
#region 成员变量
/// <summary>
/// <table >
/// <tr><td><b>功能描述</b>:oracle数据连接对象</td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table>
/// </summary>
protected oracleconnection connection;
/// <summary><table >
/// <tr><td><b>功能描述</b>:数据连接字符串</td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
private string connectionstring;
#endregion
#region 构造函数
/// <summary><table >
/// <tr><td><b>功能描述</b>:构造函数,使用配置文件中的默认数据连接字符串connectionstring,初始化数据连接对象 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
public dbobject()
{
connectionstring = configurationsettings.appsettings.get("connectionstring");//从web.config中取得的连接字符串
connection = new oracleconnection(connectionstring);
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:构造函数,根据指定的数据连接字符串,初始化数据连接对象</td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="newconnectionstring">数据连接字符串</param>
public dbobject( string newconnectionstring )
{
connectionstring = newconnectionstring;
connection = new oracleconnection( connectionstring );
}
#endregion
#region 私有方法
/// <summary><table >
/// <tr><td><b>功能描述</b>:创建一个oraclecommand对象,用于生成oracledatareader </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">存储过程的参数对象列表(数组)</param>
/// <returns>oraclecommand对象</returns>
private oraclecommand buildcommand(string storedprocname, idataparameter[] parameters)
{
oraclecommand command = new oraclecommand( storedprocname, connection );
command.commandtype = commandtype.storedprocedure;
foreach (oracleparameter parameter in parameters)
{
command.parameters.add( parameter );
}
return command;
}
#endregion
#region 运行存储过程
/// <summary>
/// <table >
/// <tr><td><b>功能描述</b>:运行存储过程,获取影响行数,返回存储过程运行结果 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table>
/// </summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">存储过程的参数对象列表(数组)</param>
/// <param name="rowsaffected">出参:执行存储过程所影响的记录行数</param>
/// <returns>存储过程的运行结果</returns>
public object runprocedure(string storedprocname, idataparameter[] parameters, out int rowsaffected )
{
object result;
//if(connection.state.tostring() == "closed") connection.open();
connection.open();
oraclecommand command = buildcommand( storedprocname, parameters );
rowsaffected = command.executenonquery();
//如果有"returnvalue"参数则返回值,否则返回null
bool blnhasreturn = false;
for (int i=0;i<parameters.length;i++)
{
if (parameters[i].direction == parameterdirection.returnvalue)
{
blnhasreturn = true;
break;
}
}
if (blnhasreturn)
result = command.parameters["returnvalue"].value;
else
result = null;
connection.close();
return result;
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行存储过程,返回产生的oracledatareader对象 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">存储过程的参数对象列表(数组)</param>
/// <returns>oracledatareader对象</returns>
public oracledatareader runprocedure(string storedprocname, idataparameter[] parameters )
{
oracledatareader returnreader;
connection.open();
oraclecommand command = buildcommand( storedprocname, parameters );
command.commandtype = commandtype.storedprocedure;
returnreader = command.executereader();
//connection.close();
return returnreader;
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行存储过程,创建一个dataset对象,
/// 将运行结果存入指定的datatable中,返回dataset对象 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">存储过程的参数对象列表(数组)</param>
/// <param name="tablename">数据表名称</param>
/// <returns>dataset对象</returns>
public dataset runprocedure(string storedprocname, idataparameter[] parameters, string tablename )
{
dataset dataset = new dataset();
connection.open();
oracledataadapter sqlda = new oracledataadapter();
sqlda.selectcommand = buildcommand( storedprocname, parameters );
sqlda.fill( dataset, tablename );
connection.close();
return dataset;
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行存储过程,将运行结果存入已有dataset对象的指定表中,无返回值 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="storedprocname">存储过程名称</param>
/// <param name="parameters">存储过程的参数对象列表(数组)</param>
/// <param name="dataset">dataset对象</param>
/// <param name="tablename">数据表名称</param>
public void runprocedure(string storedprocname, idataparameter[] parameters, dataset dataset, string tablename )
{
connection.open();
oracledataadapter sqlda = new oracledataadapter();
sqlda.selectcommand = buildcommand( storedprocname, parameters );
sqlda.fill( dataset, tablename );
connection.close();
}
#endregion
#region 运行sql语句
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行与写数据库相关的sql语句,返回影响行数 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>影响行数</returns>
public int execnonquery(string sqlstring)
{
int rowaffected;
//if(connection.state.tostring() == "closed") connection.open();
connection.open();
oraclecommand command = new oraclecommand( sqlstring, connection );
rowaffected = command.executenonquery();
connection.close();
return rowaffected;
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行sql语句,返回oracledatareader对象 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>sqldatareader对象</returns>
public oracledatareader execsqlstring(string sqlstring)
{
oracledatareader returnreader;
//if(connection.state.tostring() == "closed") connection.open();
connection.open();
oraclecommand command = new oraclecommand( sqlstring, connection );
returnreader = command.executereader();
//connection.close();
return returnreader;
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行sql语句,返回dataset对象 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="string">sql语句</param>
/// <param name="tablename">数据表名称</param>
/// <returns>dataset对象</returns>
public dataset execsqlstring(string sqlstring, string tablename )
{
dataset dataset = new dataset();
//if (connection.state.tostring() == "closed") connection.open();
connection.open();
oracledataadapter sqlda = new oracledataadapter();
sqlda.selectcommand = new oraclecommand( sqlstring, connection );
sqlda.fill( dataset, tablename );
connection.close();
return dataset;
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行sql语句,将运行结果存入已有dataset对象的指定表中,无返回值 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlstring">sql语句</param>
/// <param name="dataset">dataset对象</param>
/// <param name="tablename">数据表名称</param>
public void execsqlstring(string sqlstring, dataset dataset, string tablename )
{
//if (connection.state.tostring() == "closed") connection.open();
connection.open();
oracledataadapter sqlda = new oracledataadapter();
sqlda.selectcommand = new oraclecommand( sqlstring, connection );
sqlda.fill( dataset, tablename );
connection.close();
}
/// <summary><table >
/// <tr><td><b>功能描述</b>:运行sql语句,返回查询结果的第一行的第一列,忽略其它行或列 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>影响行数</returns>
public object execscalar(string sqlstring)
{
object returnscalar;
//if (connection.state.tostring() == "closed") connection.open();
connection.open();
oraclecommand command = new oraclecommand( sqlstring, connection );
returnscalar = command.executescalar();
//connection.close();
return returnscalar;
}
#endregion
#region 关闭数据连接
/// <summary><table >
/// <tr><td><b>功能描述</b>:关闭数据连接 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
public void close()
{
if(connection.state.tostring() == "open")
connection.close();
}
#endregion
#region 析构函数
/// <summary><table >
/// <tr><td><b>功能描述</b>:析构函数,善后处理,释放数据连接 </td></tr>
/// <tr><td><b>创 建 人</b>:夏春涛 </td></tr>
/// <tr><td><b>创建时间</b>:2005-05-28 </td></tr>
/// </table></summary>
~dbobject()
{
if(connection.state.tostring() == "open")
connection.close();
connection.dispose();
}
#endregion
}
}
新闻热点
疑难解答