<connectionStrings>
<add name="chrsym" connectionString="Data Source=192.168.1.10;UID=user;PWD=888888;DATABASE="data" providerName="System.Data.SqlClient"/>
<add name="oracle" connectionString="User ID=cs;Password=cs;Data Source=(DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST=192.168.1.10)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=cs)))" providerName="System.Data.OracleClient"/>
</connectionStrings>
.net 中引用 System.Data.OracleClient;
- using System.Data;
- using System.Data.OracleClient;
- using System.Configuration;
- using System.Collections;
- namespace Entity
- {
- /// <summary>
- /// 操作Oracle数据库
- /// </summary>
- public class OracleHelper
- {
- public OracleConnection conn = new OracleConnection();//连接对象
- protected OracleCommand cmd = new OracleCommand(); //命令对象
- // Read the connection strings from the configuration file
- //public readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["OraConnString1"].ConnectionString;
- //public readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["OraConnString2"].ConnectionString;
- //public readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["OraConnString3"].ConnectionString;
- //public readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["OraProfileConnString"].ConnectionString;
- //public readonly string ConnectionStringMembership = ConfigurationManager.ConnectionStrings["OraMembershipConnString"].ConnectionString;
- //Create a hashtable for the parameter cached
- private Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
- protected readonly string ConnectStr = ConfigurationManager.ConnectionStrings["oracle"].ToString();
- public OracleHelper()
- {
- }
- public bool OpenDB()
- {
- try
- {
- conn.ConnectionString = ConnectStr;
- cmd.Connection = conn;
- conn.Open();
- return true;
- }
- catch
- {
- return false;
- }
- }
- public bool CloseDB()
- {
- try
- {
- if (conn.State == ConnectionState.Open)
- {
- conn.Close();
- conn.Dispose();
- cmd.Dispose();
- }
- return true;
- }
- catch
- {
- return false;
- }
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// Execute a database query which does not include a select
- /// </summary>
- /// <param name="cmdType">Command type either stored procedure or SQL</param>
- /// <param name="cmdText">Acutall SQL Command</param>
- /// <param name="commandParameters">Parameters to bind to the command</param>
- /// <returns></returns>
- public int ExecuteNonQuery(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
- {
- try
- {
- PrepareCommand(null, cmdType, cmdText, commandParameters);
- int val = cmd.ExecuteNonQuery();
- cmd.Parameters.Clear();
- return val;
- }
- finally
- {
- CloseDB();
- }
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// Execute a select query that will return a result set
- /// </summary>
- /// <param name="connString">Connection string</param>
- <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
- /// <param name="commandText">the stored procedure name or PL/SQL command</param>
- /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
- /// <returns></returns>
- public OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
- {
- try
- {
- //Prepare the command to execute
- PrepareCommand(null, cmdType, cmdText, commandParameters);
- OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
- cmd.Parameters.Clear();
- return rdr;
- }
- catch
- {
- conn.Close();
- throw;
- }
- finally
- {
- }
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// Execute an OracleCommand that returns the first column of the first record against the database specified in the connection string
- /// using the provided parameters.
- /// </summary>
- /// <remarks>
- /// e.g.:
- /// Object obj = ExecuteScalar(CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));
- /// </remarks>
- /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
- /// <param name="commandText">the stored procedure name or PL/SQL command</param>
- /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>
- /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
- public object ExecuteScalar(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
- {
- try
- {
- PrepareCommand(null, cmdType, cmdText, commandParameters);
- object val = cmd.ExecuteScalar();
- cmd.Parameters.Clear();
- return val;
- }
- finally
- {
- CloseDB();
- }
- }
- /// <summary>
- /// 执行一条返回结果集的SqlCommand命令,通过已有的连接字符串。
- /// 使用参数数组提供参数
- /// </summary>
- /// <remarks>
- /// 使用示例:
- /// DateSet ds = ExecuteDataSet(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
- /// </remarks>
- /// <param name="cmdType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param>
- /// <param name="cmdText">存储过程的名字或者 T-SQL 语句</param>
- /// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param>
- /// <returns>返回一个包含结果的DataSet</returns>
- public DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
- {
- try
- {
- PrepareCommand(null, cmdType, cmdText, commandParameters);
- OracleDataAdapter adapter = new OracleDataAdapter(cmd);
- DataSet ds = new DataSet();
- adapter.Fill(ds);
- return ds;
- }
- finally
- {
- cmd.Parameters.Clear();
- CloseDB();
- }
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// Add a set of parameters to the cached
- /// </summary>
- /// <param name="cacheKey">Key value to look up the parameters</param>
- /// <param name="commandParameters">Actual parameters to cached</param>
- public void CacheParameters(string cacheKey, params OracleParameter[] commandParameters)
- {
- parmCache[cacheKey] = commandParameters;
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// Fetch parameters from the cache
- /// </summary>
- /// <param name="cacheKey">Key to look up the parameters</param>
- /// <returns></returns>
- public OracleParameter[] GetCachedParameters(string cacheKey)
- {
- OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey];
- if (cachedParms == null)
- return null;
- // 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>
- /// Internal function to prepare a command for execution by the database
- /// </summary>
- /// <param name="cmd">Existing command object</param>
- /// <param name="conn">Database connection object</param>
- /// <param name="trans">Optional transaction object</param>
- /// <param name="cmdType">Command type, e.g. stored procedure</param>
- /// <param name="cmdText">Command test</param>
- /// <param name="commandParameters">Parameters for the command</param>
- private void PrepareCommand(OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
- {
- OpenDB();
- 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>
- /// Converter to use boolean data type with Oracle
- /// </summary>
- /// <param name="value">Value to convert</param>
- /// <returns></returns>
- public string OraBit(bool value)
- {
- if (value)
- return "Y";
- else
- return "N";
- }
- /**/
- /**/
- /**/
- /// <summary>
- /// Converter to use boolean data type with Oracle
- /// </summary>
- /// <param name="value">Value to convert</param>
- /// <returns></returns>
- public bool OraBool(string value)
- {
- if (value.Equals("Y"))
- return true;
- else
- return false;
- }
- }
- }