C#连接Oracle数据库基本类

C#用来连接oracle数据库的基本类:

  1   1 using System;
  2   2 using System.Collections.Generic;
  3   3 using System.Linq;
  4   4 using System.Text;
  5   5 using System.Configuration;
  6   6 using System.Data;
  7   7 using Oracle.DataAccess.Client;
  8   8 
  9   9 namespace DLL.Base
 10  10 {
 11  11     /// <summary>
 12  12     /// oracle数据库基础类
 13  13     /// </summary>
 14  14     public class OracleDBContent
 15  15     {
 16  16         #region 私有
 17  17         #region 私有属性
 18  18         /// <summary>
 19  19         /// 获取数据库连接字符串
 20  20         /// </summary>
 21  22         private static string _connectstring = ConfigurationManager.ConnectionStrings["OraConn"].ConnectionString;
 22  23         #endregion
 23  24 
 24  25         #region 私有方法
 25  26         /// <summary>
 26  27         /// 获取数据(查询)
 27  28         /// </summary>
 28  29         /// <param name="sql">要执行的SQL语句</param>
 29  30         /// <returns></returns>
 30  31         private DataSet _GET(string sql)
 31  32         {
 32  33             try
 33  34             {
 34  35                 DataSet dt = new DataSet();
 35  36                 OracleConnection connect = new OracleConnection(_connectstring);
 36  37                 connect.Open();
 37  38                 OracleDataAdapter db = new OracleDataAdapter(sql, connect);
 38  39                 db.Fill(dt);
 39  40                 connect.Close();
 40  41                 return dt;
 41  42             }
 42  43             catch (Exception ex)
 43  44             {
 44  45                 throw new Exception(ex.Message);
 45  46             }
 46  47         }
 47  48 
 48  49         /// <summary>
 49  50         /// 执行SQl语句(增、删、改)
 50  51         /// </summary>
 51  52         /// <param name="sql">要执行的SQl语句</param>
 52  53         /// <returns></returns>
 53  54         private bool _EXCUTE(string sql)
 54  55         {
 55  56             try
 56  57             {
 57  58                 OracleConnection connect = new OracleConnection(_connectstring);
 58  59                 OracleCommand cmd = new OracleCommand();
 59  60                 cmd.Connection = connect;
 60  61                 cmd.CommandText = sql;
 61  62                 connect.Open();
 62  63                 cmd.ExecuteNonQuery();
 63  64                 connect.Close();
 64  65                 return true;
 65  66             }
 66  67             catch (Exception ex)
 67  68             {
 68  69                 throw new Exception(ex.Message);
 69  70             }
 70  71         }
 71  72         #endregion
 72  73         #endregion
 73  74 
 74  75         #region 公共方法
 75  76         /// <summary>
 76  77         /// 查询
 77  78         /// </summary>
 78  79         /// <param name="sql">数据库查询语句</param>
 79  80         /// <returns></returns>
 80  81         public virtual DataSet GetData(string sql)
 81  82         {
 82  83             try
 83  84             {
 84  85                 string sqlstr = sql;
 85  86                 return _GET(sqlstr);
 86  87             }
 87  88             catch (Exception ex)
 88  89             {
 89  90                 throw new Exception(ex.Message);
 90  91             }
 91  92         }
 92  93 
 93  94         /// <summary>
 94  95         /// 新增
 95  96         /// </summary>
 96  97         /// <param name="sql">新增语句</param>
 97  98         /// <returns></returns>
 98  99         public virtual bool Insert(string sql)
 99 100         {
100 101             try
101 102             {
102 103                 return _EXCUTE(sql);
103 104             }
104 105             catch (Exception ex)
105 106             {
106 107                 throw new Exception(ex.Message);
107 108             }
108 109         }
109 110 
110 111         /// <summary>
111 112         /// 删除
112 113         /// </summary>
113 114         /// <param name="sql">删除语句</param>
114 115         /// <returns></returns>
115 116         public virtual bool Delete(string sql)
116 117         {
117 118             try
118 119             {
119 120                 return _EXCUTE(sql);
120 121             }
121 122             catch (Exception ex)
122 123             {
123 124                 throw new Exception(ex.Message);
124 125             }
125 126         }
126 127 
127 128         /// <summary>
128 129         /// 更新
129 130         /// </summary>
130 131         /// <param name="sql">更新语句</param>
131 132         /// <returns></returns>
132 133         public virtual bool Update(string sql)
133 134         {
134 135             try
135 136             {
136 137                 return _EXCUTE(sql);
137 138             }
138 139             catch (Exception ex)
139 140             {
140 141                 throw new Exception(ex.Message);
141 142             }
142 143         }
143 144         #endregion
144 145     }
145 146 }View Code
View Code

(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
View Code

 

posted @ 2015-05-07 10:54  義劍寒風  阅读(723)  评论(0编辑  收藏  举报