C#连接Oracle数据库基本类
C#用来连接oracle数据库的基本类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Configuration; 6 using System.Data; 7 using Oracle.DataAccess.Client; 8 9 namespace DLL.Base 10 { 11 /// <summary> 12 /// oracle数据库基础类 13 /// </summary> 14 public class OracleDBContent 15 { 16 #region 私有 17 #region 私有属性 18 /// <summary> 19 /// 获取数据库连接字符串 20 /// </summary> 21 //private static string _connectstring = "Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.0.36.10)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=HYP)));User Id=xorder;Password=prod_xo_2015;"; 22 private static string _connectstring = ConfigurationManager.ConnectionStrings["OraConn"].ConnectionString; 23 #endregion 24 25 #region 私有方法 26 /// <summary> 27 /// 获取数据(查询) 28 /// </summary> 29 /// <param name="sql">要执行的SQL语句</param> 30 /// <returns></returns> 31 private DataSet _GET(string sql) 32 { 33 try 34 { 35 DataSet dt = new DataSet(); 36 OracleConnection connect = new OracleConnection(_connectstring); 37 connect.Open(); 38 OracleDataAdapter db = new OracleDataAdapter(sql, connect); 39 db.Fill(dt); 40 connect.Close(); 41 return dt; 42 } 43 catch (Exception ex) 44 { 45 throw new Exception(ex.Message); 46 } 47 } 48 49 /// <summary> 50 /// 执行SQl语句(增、删、改) 51 /// </summary> 52 /// <param name="sql">要执行的SQl语句</param> 53 /// <returns></returns> 54 private bool _EXCUTE(string sql) 55 { 56 try 57 { 58 OracleConnection connect = new OracleConnection(_connectstring); 59 OracleCommand cmd = new OracleCommand(); 60 cmd.Connection = connect; 61 cmd.CommandText = sql; 62 connect.Open(); 63 cmd.ExecuteNonQuery(); 64 connect.Close(); 65 return true; 66 } 67 catch (Exception ex) 68 { 69 throw new Exception(ex.Message); 70 } 71 } 72 #endregion 73 #endregion 74 75 #region 公共方法 76 /// <summary> 77 /// 查询 78 /// </summary> 79 /// <param name="sql">数据库查询语句</param> 80 /// <returns></returns> 81 public virtual DataSet GetData(string sql) 82 { 83 try 84 { 85 string sqlstr = sql; 86 return _GET(sqlstr); 87 } 88 catch (Exception ex) 89 { 90 throw new Exception(ex.Message); 91 } 92 } 93 94 /// <summary> 95 /// 新增 96 /// </summary> 97 /// <param name="sql">新增语句</param> 98 /// <returns></returns> 99 public virtual bool Insert(string sql) 100 { 101 try 102 { 103 return _EXCUTE(sql); 104 } 105 catch (Exception ex) 106 { 107 throw new Exception(ex.Message); 108 } 109 } 110 111 /// <summary> 112 /// 删除 113 /// </summary> 114 /// <param name="sql">删除语句</param> 115 /// <returns></returns> 116 public virtual bool Delete(string sql) 117 { 118 try 119 { 120 return _EXCUTE(sql); 121 } 122 catch (Exception ex) 123 { 124 throw new Exception(ex.Message); 125 } 126 } 127 128 /// <summary> 129 /// 更新 130 /// </summary> 131 /// <param name="sql">更新语句</param> 132 /// <returns></returns> 133 public virtual bool Update(string sql) 134 { 135 try 136 { 137 return _EXCUTE(sql); 138 } 139 catch (Exception ex) 140 { 141 throw new Exception(ex.Message); 142 } 143 } 144 #endregion 145 } 146 }
(Web.config或App.config)配置文件中数据库连接的配置:
1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3 <connectionStrings> 4 <!--测试数据库--> 7 <add name="OraConn" connectionString="Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.172)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=Test)));User Id=sa;Password=test123;"/> 9 </connectionStrings> 10 <system.web> 11 <compilation debug="true" targetFramework="4.0" /> 12 </system.web> 13 <system.serviceModel> 14 <behaviors> 15 <serviceBehaviors> 16 <behavior> 17 <!-- 为避免泄漏元数据信息,请在部署前将以下值设置为 false 并删除上面的元数据终结点 --> 18 <serviceMetadata httpGetEnabled="true"/> 19 <!-- 要接收故障异常详细信息以进行调试,请将以下值设置为 true。在部署前设置为 false 以避免泄漏异常信息 --> 20 <serviceDebug includeExceptionDetailInFaults="false"/> 21 </behavior> 22 </serviceBehaviors> 23 </behaviors> 24 <serviceHostingEnvironment multipleSiteBindingsEnabled="true" /> 25 </system.serviceModel> 26 <system.webServer> 27 <modules runAllManagedModulesForAllRequests="true"/> 28 </system.webServer> 29 30 </configuration>View Code
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。