因为访问数据库的需要,写了一个类,把访问Oracle的步骤封装了下,自己记录下,以后用的时候就可以直接Copy了。
OracleHelper
1
using
System;
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.OracleClient;
11
12 /// <summary>
13 /// OracleHelper 的摘要说明
14 /// </summary>
15 public class OracleHelper
16 {
17 public OracleHelper()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 private static string _connectionString = "" ;
25 private static OracleConnection conn;
26 private static OracleCommand comm;
27 private static OracleDataAdapter adpter;
28
29 /// <summary>
30 /// 数据库联接字符串
31 /// </summary>
32 /// <returns></returns>
33 public static string ConnectionString
34 {
35 get
36 {
37 if (_connectionString == "" )
38 {
39 // 从Web.Config中获取数据库连接字符串
40 _connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[ " OracleConnectString " ].ConnectionString;
41 }
42 return _connectionString;
43 }
44 }
45
46 /// <summary>
47 /// 打开数据库
48 /// </summary>
49 public static void OpenConnection()
50 {
51 conn = new OracleConnection(ConnectionString);
52 comm = new OracleCommand();
53 adpter = new OracleDataAdapter();
54 comm.Connection = conn;
55 adpter.SelectCommand = comm;
56 comm.CommandType = CommandType.Text;
57
58 try
59 {
60 conn.Open();
61 }
62 catch (Exception ex)
63 {
64
65 }
66 }
67
68 /// <summary>
69 /// 关闭数据库
70 /// </summary>
71 public static void CloseConection()
72 {
73 if (conn.State == ConnectionState.Open)
74 conn.Close();
75 conn = null ;
76 comm = null ;
77 adpter = null ;
78 }
79
80 /// <summary>
81 /// 执行sql语句并填充数据集
82 /// </summary>
83 /// <param name="sqlstring"></param>
84 /// <param name="dataset"></param>
85 /// <param name="tbname"></param>
86 /// <returns></returns>
87 public static bool ExecuteSqlAndFillDataSet( string sqlstring, ref DataSet dataset, string tbname)
88 {
89 try
90 {
91 comm.CommandText = sqlstring;
92 adpter.Fill(dataset,tbname);
93 }
94 catch (Exception ex)
95 {
96 return false ;
97 }
98
99 return true ;
100 }
101
102 /// <summary>
103 /// 执行SQL语句
104 /// </summary>
105 /// <param name="sqlstring"></param>
106 /// <returns></returns>
107 public static bool ExecSql( string sqlstring)
108 {
109 try
110 {
111 comm.CommandText = sqlstring;
112 comm.ExecuteNonQuery();
113 }
114 catch (Exception ex)
115 {
116 return false ;
117 }
118 return true ;
119 }
120
121 /// <summary>
122 /// 执行Sql,返回DataReader
123 /// </summary>
124 /// <param name="sqlstring"></param>
125 /// <returns></returns>
126
127 public static OracleDataReader ExecQuerySql( string sqlstring)
128 {
129 try
130 {
131 comm.CommandText = sqlstring;
132 return comm.ExecuteReader();
133 }
134 catch (Exception ex)
135 {
136 return null ;
137 }
138 }
139 }
140
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10 using System.Data.OracleClient;
11
12 /// <summary>
13 /// OracleHelper 的摘要说明
14 /// </summary>
15 public class OracleHelper
16 {
17 public OracleHelper()
18 {
19 //
20 // TODO: 在此处添加构造函数逻辑
21 //
22 }
23
24 private static string _connectionString = "" ;
25 private static OracleConnection conn;
26 private static OracleCommand comm;
27 private static OracleDataAdapter adpter;
28
29 /// <summary>
30 /// 数据库联接字符串
31 /// </summary>
32 /// <returns></returns>
33 public static string ConnectionString
34 {
35 get
36 {
37 if (_connectionString == "" )
38 {
39 // 从Web.Config中获取数据库连接字符串
40 _connectionString = System.Configuration.ConfigurationManager.ConnectionStrings[ " OracleConnectString " ].ConnectionString;
41 }
42 return _connectionString;
43 }
44 }
45
46 /// <summary>
47 /// 打开数据库
48 /// </summary>
49 public static void OpenConnection()
50 {
51 conn = new OracleConnection(ConnectionString);
52 comm = new OracleCommand();
53 adpter = new OracleDataAdapter();
54 comm.Connection = conn;
55 adpter.SelectCommand = comm;
56 comm.CommandType = CommandType.Text;
57
58 try
59 {
60 conn.Open();
61 }
62 catch (Exception ex)
63 {
64
65 }
66 }
67
68 /// <summary>
69 /// 关闭数据库
70 /// </summary>
71 public static void CloseConection()
72 {
73 if (conn.State == ConnectionState.Open)
74 conn.Close();
75 conn = null ;
76 comm = null ;
77 adpter = null ;
78 }
79
80 /// <summary>
81 /// 执行sql语句并填充数据集
82 /// </summary>
83 /// <param name="sqlstring"></param>
84 /// <param name="dataset"></param>
85 /// <param name="tbname"></param>
86 /// <returns></returns>
87 public static bool ExecuteSqlAndFillDataSet( string sqlstring, ref DataSet dataset, string tbname)
88 {
89 try
90 {
91 comm.CommandText = sqlstring;
92 adpter.Fill(dataset,tbname);
93 }
94 catch (Exception ex)
95 {
96 return false ;
97 }
98
99 return true ;
100 }
101
102 /// <summary>
103 /// 执行SQL语句
104 /// </summary>
105 /// <param name="sqlstring"></param>
106 /// <returns></returns>
107 public static bool ExecSql( string sqlstring)
108 {
109 try
110 {
111 comm.CommandText = sqlstring;
112 comm.ExecuteNonQuery();
113 }
114 catch (Exception ex)
115 {
116 return false ;
117 }
118 return true ;
119 }
120
121 /// <summary>
122 /// 执行Sql,返回DataReader
123 /// </summary>
124 /// <param name="sqlstring"></param>
125 /// <returns></returns>
126
127 public static OracleDataReader ExecQuerySql( string sqlstring)
128 {
129 try
130 {
131 comm.CommandText = sqlstring;
132 return comm.ExecuteReader();
133 }
134 catch (Exception ex)
135 {
136 return null ;
137 }
138 }
139 }
140
Web.Config配置文件:
<
connectionStrings
>
< add name = " OracleConnectString " connectionString = " Data Source=edms;UID=edms;PWD=edms " providerName = " System.Data.OracleClient " />
</ connectionStrings >
< add name = " OracleConnectString " connectionString = " Data Source=edms;UID=edms;PWD=edms " providerName = " System.Data.OracleClient " />
</ connectionStrings >