oracle 连接字符串

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

 

[csharp]  view plain copy
  1. using System.Data;  
  2. using System.Data.OracleClient;  
  3. using System.Configuration;  
  4. using System.Collections;  
  5.   
  6.   
  7. namespace Entity  
  8. {  
  9.     /// <summary>  
  10.     /// 操作Oracle数据库  
  11.     /// </summary>  
  12.   
  13.     public class OracleHelper  
  14.     {  
  15.         public OracleConnection conn = new OracleConnection();//连接对象  
  16.         protected OracleCommand cmd = new OracleCommand();  //命令对象  
  17.         // Read the connection strings from the configuration file  
  18.         //public readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["OraConnString1"].ConnectionString;  
  19.         //public readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["OraConnString2"].ConnectionString;  
  20.         //public readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["OraConnString3"].ConnectionString;  
  21.         //public readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["OraProfileConnString"].ConnectionString;  
  22.         //public readonly string ConnectionStringMembership = ConfigurationManager.ConnectionStrings["OraMembershipConnString"].ConnectionString;  
  23.   
  24.         //Create a hashtable for the parameter cached  
  25.         private Hashtable parmCache = Hashtable.Synchronized(new Hashtable());  
  26.         protected readonly string ConnectStr = ConfigurationManager.ConnectionStrings["oracle"].ToString();  
  27.           
  28.   
  29.         public OracleHelper()  
  30.         {  
  31.              
  32.         }  
  33.   
  34.         public bool OpenDB()  
  35.         {  
  36.             try  
  37.             {  
  38.                 conn.ConnectionString = ConnectStr;  
  39.                 cmd.Connection = conn;  
  40.                 conn.Open();  
  41.                 return true;  
  42.             }  
  43.             catch  
  44.             {  
  45.                 return false;  
  46.             }  
  47.         }  
  48.   
  49.         public bool CloseDB()  
  50.         {  
  51.             try  
  52.             {  
  53.                 if (conn.State == ConnectionState.Open)  
  54.                 {  
  55.                     conn.Close();  
  56.                     conn.Dispose();  
  57.                     cmd.Dispose();  
  58.                 }  
  59.                 return true;  
  60.             }  
  61.             catch  
  62.             {  
  63.                 return false;  
  64.             }  
  65.         }  
  66.           
  67.         /**/  
  68.         /**/  
  69.         /**/  
  70.         /// <summary>  
  71.         /// Execute a database query which does not include a select  
  72.         /// </summary>  
  73.         /// <param name="cmdType">Command type either stored procedure or SQL</param>  
  74.         /// <param name="cmdText">Acutall SQL Command</param>  
  75.         /// <param name="commandParameters">Parameters to bind to the command</param>  
  76.         /// <returns></returns>  
  77.         public int ExecuteNonQuery(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
  78.         {  
  79.             try  
  80.             {  
  81.                 PrepareCommand(null, cmdType, cmdText, commandParameters);  
  82.                 int val = cmd.ExecuteNonQuery();  
  83.                 cmd.Parameters.Clear();  
  84.                 return val;  
  85.             }  
  86.             finally  
  87.             {  
  88.                 CloseDB();  
  89.             }  
  90.         }  
  91.   
  92.   
  93.         /**/  
  94.         /**/  
  95.         /**/  
  96.         /// <summary>  
  97.         /// Execute a select query that will return a result set  
  98.         /// </summary>  
  99.         /// <param name="connString">Connection string</param>  
  100.          <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  101.         /// <param name="commandText">the stored procedure name or PL/SQL command</param>  
  102.         /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>  
  103.         /// <returns></returns>  
  104.         public OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
  105.         {  
  106.             try  
  107.             {  
  108.                 //Prepare the command to execute  
  109.                 PrepareCommand(null, cmdType, cmdText, commandParameters);  
  110.                 OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);  
  111.                 cmd.Parameters.Clear();  
  112.                 return rdr;  
  113.             }  
  114.             catch  
  115.             {  
  116.                 conn.Close();  
  117.                 throw;  
  118.             }  
  119.             finally  
  120.             {  
  121.   
  122.             }  
  123.         }  
  124.   
  125.         /**/  
  126.         /**/  
  127.         /**/  
  128.         /// <summary>  
  129.         /// Execute an OracleCommand that returns the first column of the first record against the database specified in the connection string   
  130.         /// using the provided parameters.  
  131.         /// </summary>  
  132.         /// <remarks>  
  133.         /// e.g.:    
  134.         ///  Object obj = ExecuteScalar(CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));  
  135.         /// </remarks>  
  136.         /// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>  
  137.         /// <param name="commandText">the stored procedure name or PL/SQL command</param>  
  138.         /// <param name="commandParameters">an array of OracleParamters used to execute the command</param>  
  139.         /// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>  
  140.         public object ExecuteScalar(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
  141.         {  
  142.             try  
  143.             {  
  144.                 PrepareCommand(null, cmdType, cmdText, commandParameters);  
  145.                 object val = cmd.ExecuteScalar();  
  146.                 cmd.Parameters.Clear();  
  147.                 return val;  
  148.             }  
  149.             finally  
  150.             {  
  151.                 CloseDB();  
  152.             }  
  153.         }  
  154.   
  155.         /// <summary>  
  156.         /// 执行一条返回结果集的SqlCommand命令,通过已有的连接字符串。  
  157.         /// 使用参数数组提供参数  
  158.         /// </summary>  
  159.         /// <remarks>  
  160.         /// 使用示例:   
  161.         /// DateSet ds = ExecuteDataSet(CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));  
  162.         /// </remarks>  
  163.         /// <param name="cmdType">SqlCommand命令类型 (存储过程, T-SQL语句, 等等。)</param>  
  164.         /// <param name="cmdText">存储过程的名字或者 T-SQL 语句</param>  
  165.         /// <param name="commandParameters">以数组形式提供SqlCommand命令中用到的参数列表</param>  
  166.         /// <returns>返回一个包含结果的DataSet</returns>  
  167.         public DataSet ExecuteDataSet(CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)  
  168.         {  
  169.             try  
  170.             {  
  171.                 PrepareCommand(null, cmdType, cmdText, commandParameters);  
  172.                 OracleDataAdapter adapter = new OracleDataAdapter(cmd);  
  173.                 DataSet ds = new DataSet();  
  174.                 adapter.Fill(ds);  
  175.                 return ds;  
  176.             }  
  177.             finally  
  178.             {  
  179.                 cmd.Parameters.Clear();  
  180.                 CloseDB();  
  181.             }  
  182.         }  
  183.   
  184.         /**/  
  185.         /**/  
  186.         /**/  
  187.         /// <summary>  
  188.         /// Add a set of parameters to the cached  
  189.         /// </summary>  
  190.         /// <param name="cacheKey">Key value to look up the parameters</param>  
  191.         /// <param name="commandParameters">Actual parameters to cached</param>  
  192.         public void CacheParameters(string cacheKey, params OracleParameter[] commandParameters)  
  193.         {  
  194.             parmCache[cacheKey] = commandParameters;  
  195.         }  
  196.   
  197.         /**/  
  198.         /**/  
  199.         /**/  
  200.         /// <summary>  
  201.         /// Fetch parameters from the cache  
  202.         /// </summary>  
  203.         /// <param name="cacheKey">Key to look up the parameters</param>  
  204.         /// <returns></returns>  
  205.         public OracleParameter[] GetCachedParameters(string cacheKey)  
  206.         {  
  207.             OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey];  
  208.   
  209.             if (cachedParms == null)  
  210.                 return null;  
  211.   
  212.             // If the parameters are in the cache  
  213.             OracleParameter[] clonedParms = new OracleParameter[cachedParms.Length];  
  214.   
  215.             // return a copy of the parameters  
  216.             for (int i = 0, j = cachedParms.Length; i < j; i++)  
  217.                 clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone();  
  218.   
  219.             return clonedParms;  
  220.         }  
  221.   
  222.         /**/  
  223.         /**/  
  224.         /**/  
  225.         /// <summary>  
  226.         /// Internal function to prepare a command for execution by the database  
  227.         /// </summary>  
  228.         /// <param name="cmd">Existing command object</param>  
  229.         /// <param name="conn">Database connection object</param>  
  230.         /// <param name="trans">Optional transaction object</param>  
  231.         /// <param name="cmdType">Command type, e.g. stored procedure</param>  
  232.         /// <param name="cmdText">Command test</param>  
  233.         /// <param name="commandParameters">Parameters for the command</param>  
  234.         private void PrepareCommand(OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)  
  235.         {  
  236.   
  237.             OpenDB();  
  238.             cmd.CommandText = cmdText;  
  239.             cmd.CommandType = cmdType;  
  240.   
  241.             //Bind it to the transaction if it exists  
  242.             if (trans != null)  
  243.                 cmd.Transaction = trans;  
  244.   
  245.             // Bind the parameters passed in  
  246.             if (commandParameters != null)  
  247.             {  
  248.                 foreach (OracleParameter parm in commandParameters)  
  249.                     cmd.Parameters.Add(parm);  
  250.             }  
  251.         }  
  252.   
  253.         /**/  
  254.         /**/  
  255.         /**/  
  256.         /// <summary>  
  257.         /// Converter to use boolean data type with Oracle  
  258.         /// </summary>  
  259.         /// <param name="value">Value to convert</param>  
  260.         /// <returns></returns>  
  261.         public string OraBit(bool value)  
  262.         {  
  263.             if (value)  
  264.                 return "Y";  
  265.             else  
  266.                 return "N";  
  267.         }  
  268.   
  269.         /**/  
  270.         /**/  
  271.         /**/  
  272.         /// <summary>  
  273.         /// Converter to use boolean data type with Oracle  
  274.         /// </summary>  
  275.         /// <param name="value">Value to convert</param>  
  276.         /// <returns></returns>  
  277.         public bool OraBool(string value)  
  278.         {  
  279.             if (value.Equals("Y"))  
  280.                 return true;  
  281.             else  
  282.                 return false;  
  283.         }  
  284.     }  
  285. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值