<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=dev;User ID=username ; Password=password; Omit Oracle Connection Name=True; enlist=false" providerName="System.Data.OracleClient"/>
</connectionStrings>
using System;
using System.Collections.Generic;
using System.Text;
namespace Common.Utility
{
///<summary>
/// --------------------------------------------------------------
/// Description: 数据据基本操作接口
/// --------------------------------------------------------------
///</summary>
///<typeparam name="T">实体对象</typeparam>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
publicinterface IBaseOperate<T>
{
///<summary>
/// 添加一条记录到表格中
///</summary>
///<param name="model">实体对象</param>
///<returns>返回数据库受影响的记录数</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
int Insert(T model );
///<summary>
/// 更新表格中的一条记录
///</summary>
///<param name="model">实体对象</param>
///<returns>返回数据库受影响的记录数</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
int Update(T model);
///<summary>
/// 删除表格中的一条记录
///</summary>
///<param name="model">实体对象</param>
///<returns>返回数据库受影响的记录数</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
int Delete(T model);
///<summary>
/// 根据主键获取一条记录
///</summary>
///<param name="id">表主键记录ID</param>
///<returns>实体对象</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
T GetModel(string id);
///<summary>
/// 获取此表中所有记录
///</summary>
///<returns>实体对象记录List对象</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
IList<T> GetList();
///<summary>
/// 某条记录是否存在
///</summary>
///<param name="id">表主键记录ID</param>
///<returns>true 存在此ID,false不存在此ID</returns>
//[Obsolete("此接口不再被支持请使用IDataBaseOperate<T>", false)]
bool Exists(string id);
}
}
using System;
using System.Configuration;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
namespace Dtsc.Common.Utility
{
///<summary>
/// --------------------------------------------------------------
/// Description: Oracle数据库访问类
/// --------------------------------------------------------------
///</summary>
publicabstractclass OracleHelper
{
///<summary>
/// 数据库连接字符串
///</summary>
publicstaticstring ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//public static readonly string ConnectionString = "Data Source=dtscsmis;User ID=dtscsmis; Password=dtscsmis";
///<summary>
///
///</summary>
privatestatic Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
#region ExecuteNonQuery
///<summary>
/// Executes the non query.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
using (OracleConnection connection =new OracleConnection(ConnectionString))
{
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
///<summary>
/// Executes the non query.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteNonQuery(CommandType.Text, cmdText, commandParameters);
}
///<summary>
/// Executes the non query.
///</summary>
///<param name="trans">The trans.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
///<summary>
/// Executes the non query.
///</summary>
///<param name="connection">The connection.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticint ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
#endregion
#region ExecuteReader
///<summary>
/// Executes the reader.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic OracleDataReader ExecuteReader(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
OracleConnection conn =new OracleConnection(ConnectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch(Exception exc)
{
conn.Close();
throw exc;
}
}
///<summary>
/// Executes the reader.
///</summary>
///<param name="conn">数据库连接</param>
///<param name="cmdType">命令类型</param>
///<param name="cmdText">命令内容</param>
///<param name="commandParameters">命令参数</param>
///<returns>reader</returns>
publicstatic OracleDataReader ExecuteReader(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception exc)
{
conn.Close();
throw exc;
}
}
///<summary>
/// 执行数据读取器(当DataReader关闭时,相关的数据库连接不关闭)
///</summary>
///<param name="conn">数据库连接</param>
///<param name="cmdType">命令类型</param>
///<param name="cmdText">命令内容</param>
///<param name="commandParameters">命令参数</param>
///<returns>reader</returns>
publicstatic OracleDataReader ExecuteReaderWithoutClosingConnection(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
OracleDataReader rdr = cmd.ExecuteReader();
cmd.Parameters.Clear();
return rdr;
}
catch (Exception exc)
{
conn.Close();
throw exc;
}
}
///<summary>
/// Executes the reader.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic OracleDataReader ExecuteReader(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteReader(CommandType.Text, cmdText, commandParameters);
}
#endregion
///<summary>
/// Executes the data set.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic DataSet ExecuteDataSet(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteDataSet(CommandType.Text, cmdText, commandParameters);
}
///<summary>
/// Executes the data set.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstatic DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
using (OracleConnection conn =new OracleConnection(ConnectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
OracleDataAdapter oda =new OracleDataAdapter(cmd);
DataSet ds =new DataSet();
oda.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
}
#region ExecuteScalar
///<summary>
/// Executes the scalar.
///</summary>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
using (OracleConnection conn =new OracleConnection(ConnectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
///<summary>
/// Executes the scalar.
///</summary>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(string cmdText, params OracleParameter[] commandParameters)
{
return ExecuteScalar(CommandType.Text, cmdText, commandParameters);
}
///<summary>
/// Executes the scalar.
///</summary>
///<param name="transaction">The transaction.</param>
///<param name="commandType">Type of the command.</param>
///<param name="commandText">The command text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
{
if (transaction ==null)
thrownew ArgumentNullException("transaction");
if (transaction !=null&& transaction.Connection ==null)
thrownew ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");
// Create a command and prepare it for execution
OracleCommand cmd =new OracleCommand();
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);
// Execute the command & return the results
object retval = cmd.ExecuteScalar();
// Detach the SqlParameters from the command object, so they can be used again
cmd.Parameters.Clear();
return retval;
}
///<summary>
/// Executes the scalar.
///</summary>
///<param name="connectionString">The connection string.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
///<returns></returns>
publicstaticobject ExecuteScalar(OracleConnection connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{
OracleCommand cmd =new OracleCommand();
PrepareCommand(cmd, connectionString, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
#endregion
///<summary>
/// Caches the parameters.
///</summary>
///<param name="cacheKey">The cache key.</param>
///<param name="commandParameters">The command parameters.</param>
publicstaticvoid CacheParameters(string cacheKey, params OracleParameter[] commandParameters)
{
parmCache[cacheKey] = commandParameters;
}
///<summary>
/// Gets the cached parameters.
///</summary>
///<param name="cacheKey">The cache key.</param>
///<returns></returns>
publicstatic OracleParameter[] GetCachedParameters(string cacheKey)
{
OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey];
if (cachedParms ==null)
returnnull;
// If the parameters are in the cache
OracleParameter[] clonedParms =new OracleParameter[cachedParms.Length];
// return a copy of the parameters
for (int i =0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
///<summary>
/// Prepares the command.
///</summary>
///<param name="cmd">The CMD.</param>
///<param name="conn">The conn.</param>
///<param name="trans">The trans.</param>
///<param name="cmdType">Type of the CMD.</param>
///<param name="cmdText">The CMD text.</param>
///<param name="commandParameters">The command parameters.</param>
privatestaticvoid PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
{
//Open the connection if required
if (conn.State != ConnectionState.Open)
conn.Open();
//Set up the command
cmd.Connection = conn;
cmd.CommandText = cmdText;
cmd.CommandType = cmdType;
//Bind it to the transaction if it exists
if (trans !=null)
cmd.Transaction = trans;
// Bind the parameters passed in
if (commandParameters !=null)
{
foreach (OracleParameter parm in commandParameters)
cmd.Parameters.Add(parm);
}
}
///<summary>
/// Oras the bit.
///</summary>
///<param name="value">if set to <c>true</c> [value].</param>
///<returns></returns>
publicstaticstring OraBit(bool value)
{
if (value)
return"Y";
else
return"N";
}
///<summary>
/// Oras the bool.
///</summary>
///<param name="value">The value.</param>
///<returns></returns>
publicstaticbool OraBool(string value)
{
if (value.Equals("Y"))
returntrue;
else
returnfalse;
}
///<summary>
/// Safes the value.
///</summary>
///<param name="obj">The obj.</param>
///<returns></returns>
publicstaticobject SafeValue(object obj)
{
if (obj ==null)
return DBNull.Value;
else
return obj;
}
#region IDataBase 成员
///<summary>
/// 获得数据库连接
///</summary>
///<returns>数据库连接</returns>
publicstatic System.Data.Common.DbConnection GetConnection()
{
returnnew OracleConnection(ConnectionString);
}
#endregion
}
}