using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using System.Web.Security;
using System.Configuration;
using LC.SCS.Model;
namespace LC.SCS.Accounting.Service
{
public class OrcDBHelper
{
public static readonly string Connstr = PasswordCryp.Decrypt(ConfigurationManager.ConnectionStrings["DatabaseAddr"].ToString());
public static readonly string Connstr2 = PasswordCryp.Decrypt(ConfigurationManager.ConnectionStrings["DatabaseAddr2"].ToString());
public static OracleCommand Cmd = null;
public static OracleTransaction Trans;
static OrcDBHelper()
{
}
#region 设置OracleCommand对象
///
/// 设置OracleCommand对象
///
/// OracleCommand对象
/// 命令文本
/// 命令类型
/// 参数集合
private static void SetCommand(OracleConnection Conn, OracleCommand cmd, string cmdText, CommandType cmdType,
OracleParameter[] cmdParms)
{
cmd.Connection = Conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
if (Trans != null)
cmd.Transaction = Trans;
if (cmdParms != null)
{
cmd.Parameters.Clear();
cmd.Parameters.AddRange(cmdParms);
}
}
#endregion
#region 执行相应的sql语句,返回相应的DataSet对象
///
/// 执行相应的sql语句,返回相应的DataSet对象
///
/// sql语句
///
public static DataSet GetDataSet(string sqlstr)
{
var set = new DataSet();
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = null;
Cmd = new OracleCommand(sqlstr, Conn);
if (Trans != null)
Cmd.Transaction = Trans;
var adp = new OracleDataAdapter(Cmd);
adp.Fill(set);
if (Trans == null)
Conn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return set;
}
#endregion
#region 执行相应的sql语句,返回相应的DataSet对象
///
/// 执行相应的sql语句,返回相应的DataSet对象
///
/// sql语句
/// 表名
///
public static DataSet GetDataSet(string sqlstr, string tableName)
{
var set = new DataSet();
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand(sqlstr, Conn);
if (Trans != null)
Cmd.Transaction = Trans;
var adp = new OracleDataAdapter(Cmd);
adp.Fill(set, tableName);
if (Trans == null)
Conn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return set;
}
#endregion
#region 执行不带参数sql语句,返回所影响的行数
///
/// 执行不带参数sql语句,返回所影响的行数
///
/// 增,删,改sql语句
///
public static int ExecuteNonQuery(string cmdText)
{
int count;
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand(cmdText, Conn);
if (Trans != null)
Cmd.Transaction = Trans;
count = Cmd.ExecuteNonQuery();
if (Trans == null)
Conn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return count;
}
#endregion
#region 执行带参数sql语句或存储过程,返回所影响的行数
///
/// 执行带参数sql语句或存储过程,返回所影响的行数
///
/// 带参数的sql语句和存储过程名
/// 命令类型
/// 参数集合
///
public static int ExecuteNonQuery(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
{
int count;
OracleConnection Conn = new OracleConnection(Connstr);
try
{
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand();
if (Trans != null)
Cmd.Transaction = Trans;
SetCommand(Conn, Cmd, cmdText, cmdType, cmdParms);
count = Cmd.ExecuteNonQuery();
Cmd.Parameters.Clear();
if (Trans == null)
{
Conn.Close();
Conn.Dispose();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
Conn.Close();
Conn.Dispose();
}
//finally
//{
// Conn.Dispose();
// Conn.Close();
//}
return count;
}
#endregion
#region 执行不带参数sql语句,返回一个从数据源读取数据的OracleDataReader对象
///
/// 执行不带参数sql语句,返回一个从数据源读取数据的OracleDataReader对象
///
/// 相应的sql语句
///
public static OracleDataReader ExecuteReader(string cmdText)
{
OracleDataReader reader;
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand(cmdText, Conn);
if (Trans != null)
Cmd.Transaction = Trans;
reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return reader;
}
#endregion
#region 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的OracleDataReader对象
///
/// 执行带参数的sql语句或存储过程,返回一个从数据源读取数据的OracleDataReader对象
///
/// sql语句或存储过程名
/// 命令类型
/// 参数集合
///
public static OracleDataReader ExecuteReader(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
{
OracleDataReader reader;
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand();
if (Trans != null)
Cmd.Transaction = Trans;
SetCommand(Conn, Cmd, cmdText, cmdType, cmdParms);
reader = Cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return reader;
}
#endregion
#region 执行不带参数sql语句,返回结果集首行首列的值object
///
/// 执行不带参数sql语句,返回结果集首行首列的值object
///
/// 相应的sql语句
///
public static object ExecuteScalar(string cmdText)
{
object obj;
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand(cmdText, Conn);
if (Trans != null)
Cmd.Transaction = Trans;
obj = Cmd.ExecuteScalar();
if (Trans == null)
Conn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return obj;
}
#endregion
#region 执行带参数sql语句或存储过程,返回结果集首行首列的值object
///
/// 执行带参数sql语句或存储过程,返回结果集首行首列的值object
///
/// sql语句或存储过程名
/// 命令类型
/// 返回结果集首行首列的值object
///
public static object ExecuteScalar(string cmdText, CommandType cmdType, OracleParameter[] cmdParms)
{
object obj;
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
Cmd = new OracleCommand();
if (Trans != null)
Cmd.Transaction = Trans;
SetCommand(Conn, Cmd, cmdText, cmdType, cmdParms);
obj = Cmd.ExecuteScalar();
if (Trans == null)
Conn.Close();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return obj;
}
#endregion
///
/// 执行存储过程,返回游标集合
///
/// 存储过程名
/// 存储过程参数
///
public static DataTable DBExecStoredProcedure(string spName, OracleParameter[] sqlParme)
{
try
{
OracleConnection Conn = new OracleConnection(Connstr);
try
{
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
catch (Exception e)
{
Conn = new OracleConnection(Connstr2);
if (Conn.State != ConnectionState.Open)
Conn.Open();
}
OracleCommand OraCmd = new OracleCommand();
OraCmd.Connection = Conn;
OraCmd.CommandText = spName;
OraCmd.CommandType = CommandType.StoredProcedure;
OraCmd.Parameters.Clear();
foreach (OracleParameter parme in sqlParme)
{
OraCmd.Parameters.Add(parme);
}
DataSet dsList = new DataSet();
DateTime BegTime = System.DateTime.Now;
OracleDataAdapter adapter = new OracleDataAdapter(OraCmd);
adapter.Fill(dsList);
return dsList.Tables[0];
}
catch { return new DataTable(); }
}
}
}