using System;
using System.Configuration;
using System.Data;
using Oracle.DataAccess.Client;
#endregion
namespace WIS.Base.Data
{
///
///
/// 文 件 名:DbObject.cs
/// 功能描述:数据层基类,提供对底层数据的基本操作
/// 创 建 人:夏春涛 xchuntao@163.com qq:23106676
/// 创建时间:2005-05-28
///
///
public class DbObject
{
#region 成员变量
///
///
/// 功能描述:Oracle数据连接对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
///
protected OracleConnection Connection;
///
/// 功能描述:数据连接字符串
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
private string connectionString;
#endregion
#region 构造函数
///
/// 功能描述:构造函数,使用配置文件中的默认数据连接字符串ConnectionString,初始化数据连接对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
public DbObject()
{
connectionString = ConfigurationSettings.AppSettings.Get("ConnectionString",',',');//从Web.Config中取得的连接字符串
Connection = new OracleConnection(connectionString,',',');
}
///
/// 功能描述:构造函数,根据指定的数据连接字符串,初始化数据连接对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// 数据连接字符串
public DbObject( string newConnectionString )
{
connectionString = newConnectionString;
Connection = new OracleConnection( connectionString ,',',');
}
#endregion
#region 私有方法
///
/// 功能描述:创建一个OracleCommand对象,用于生成OracleDataReader
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
///
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 运行存储过程
///
///
/// 功能描述:运行存储过程,获取影响行数,返回存储过程运行结果
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
/// 出参:执行存储过程所影响的记录行数
///
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
if (parameters[i].Direction == ParameterDirection.ReturnValue)
{
blnHasReturn = true;
break;
}
}
if (blnHasReturn)
result = command.Parameters["ReturnValue"].Value;
else
result = null;
Connection.Close(,',',');
return result;
}
///
/// 功能描述:运行存储过程,返回产生的OracleDataReader对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
///
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;
}
///
/// 功能描述:运行存储过程,创建一个DataSet对象,
/// 将运行结果存入指定的DataTable中,返回DataSet对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
/// 数据表名称
///
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;
}
///
/// 功能描述:运行存储过程,将运行结果存入已有DataSet对象的指定表中,无返回值
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// 存储过程名称
/// 存储过程的参数对象列表(数组)
/// DataSet对象
/// 数据表名称
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语句
///
/// 功能描述:运行与写数据库相关的SQL语句,返回影响行数
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// SQL语句
///
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;
}
///
/// 功能描述:运行SQL语句,返回OracleDataReader对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// SQL语句
///
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;
}
///
/// 功能描述:运行SQL语句,返回DataSet对象
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// SQL语句
/// 数据表名称
///
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;
}
///
/// 功能描述:运行SQL语句,将运行结果存入已有DataSet对象的指定表中,无返回值
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// SQL语句
/// DataSet对象
/// 数据表名称
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(,',',');
}
///
/// 功能描述:运行SQL语句,返回查询结果的第一行的第一列,忽略其它行或列
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
/// SQL语句
///
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 关闭数据连接
///
/// 功能描述:关闭数据连接
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
public void Close()
{
if(Connection.State.ToString() == "Open")
Connection.Close(,',',');
}
#endregion
#region 析构函数
///
/// 功能描述:析构函数,善后处理,释放数据连接
/// 创 建 人:夏春涛
/// 创建时间:2005-05-28
///
~DbObject()
{
if(Connection.State.ToString() == "Open")
Connection.Close(,',',');
Connection.Dispose(,',',');
}
#endregion
}
}