#region 执行查询语句,返回SqlDataReader
/// <summary>
/// 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
/// </summary>
/// <param>查询语句</param>
/// <returns>SqlDataReader</returns>
public static OracleDataReader ExecuteReader(string strSQL)
{
OracleConnection connection = new OracleConnection(connectionString);
OracleCommand cmd = new OracleCommand(strSQL, connection);
try
{
connection.Open();
OracleDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return myReader;
}
catch (System.Data.OracleClient.OracleException e)
{
throw e;
}
finally
{
connection.Close();
}
}
#endregion
#region 执行SQL查询语句,返回DataTable数据表
/// <summary>
/// 执行SQL查询语句
/// </summary>
/// <param></param>
/// <returns>返回DataTable数据表</returns>
public static DataTable GetDataTable(string sqlStr)
{
OracleConnection mycon = new OracleConnection(connectionString);
OracleCommand mycmd = new OracleCommand(sqlStr, mycon);
DataTable dt = new DataTable();
OracleDataAdapter da = null;
try
{
mycon.Open();
da = new OracleDataAdapter(sqlStr, mycon);
da.Fill(dt);
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
mycon.Close();
}
return dt;
}
#endregion
#region 存储过程操作
/// <summary>
/// 运行存储过程,返回datatable;
/// </summary>
/// <param>存储过程名称</param>
/// <param>参数</param>
/// <returns></returns>
public static DataTable RunProcedureDatatable(string storedProcName, IDataParameter[] parameters)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
DataSet ds = new DataSet();
connection.Open();
OracleDataAdapter sqlDA = new OracleDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(ds);
connection.Close();
return ds.Tables[0];
}
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param>存储过程名称</param>
/// <param>参数</param>
/// <returns></returns>
public static int RunProcedure(string storedProcName, IDataParameter[] parameters)
{
using (OracleConnection connection = new OracleConnection(connectionString))
{
try
{
connection.Open();
OracleCommand command = new OracleCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (OracleParameter parameter in parameters)
{
if (parameter != null)
{
// 检查未分配值的输出参数,将其分配以DBNull.Value.
if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
command.Parameters.Add(parameter);
}
}
int rows = command.ExecuteNonQuery();
return rows;
}
finally
{
connection.Close();
}
}
}