ODP操作procedure

 

 

namespace WIS.Base.Data

{

 /// <summary>

 /// <table style="font-size:12px">

 /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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 style="font-size:12px">

  /// <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

 

 

 

你可能感兴趣的:(procedure)