代码
1 /**//// <summary>
2 /// 数据访问基础类(基于OleDb)
3 /// Copyright (C) 2004-2008 HOMEZZM
4 /// </summary>
5 public abstract class DbHelperOleDb
6 {
7 //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
8 public static string connectionString = PubConstant.ConnectionString;
9 public DbHelperOleDb()
10 {
11 }
12
13 执行简单SQL语句#region 执行简单SQL语句
14
15 /**//// <summary>
16 /// 执行SQL语句,返回影响的记录数
17 /// </summary>
18 /// <param name="SQLString">SQL语句</param>
19 /// <returns>影响的记录数</returns>
20 public static int ExecuteSql(string SQLString)
21 {
22 using (OleDbConnection connection = new OleDbConnection(connectionString))
23 {
24 using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
25 {
26 try
27 {
28 connection.Open();
29 int rows = cmd.ExecuteNonQuery();
30 return rows;
31 }
32 catch (System.Data.OleDb.OleDbException E)
33 {
34 connection.Close();
35 throw new Exception(E.Message);
36 }
37 }
38 }
39 }
40
41 /**//// <summary>
42 /// 执行多条SQL语句,实现数据库事务。
43 /// </summary>
44 /// <param name="SQLStringList">多条SQL语句</param>
45 public static void ExecuteSqlTran(ArrayList SQLStringList)
46 {
47 using (OleDbConnection conn = new OleDbConnection(connectionString))
48 {
49 conn.Open();
50 OleDbCommand cmd = new OleDbCommand();
51 cmd.Connection = conn;
52 OleDbTransaction tx = conn.BeginTransaction();
53 cmd.Transaction = tx;
54 try
55 {
56 for (int n = 0; n < SQLStringList.Count; n++)
57 {
58 string strsql = SQLStringList[n].ToString();
59 if (strsql.Trim().Length > 1)
60 {
61 cmd.CommandText = strsql;
62 cmd.ExecuteNonQuery();
63 }
64 }
65 tx.Commit();
66 }
67 catch (System.Data.OleDb.OleDbException E)
68 {
69 tx.Rollback();
70 throw new Exception(E.Message);
71 }
72 }
73 }
74 /**//// <summary>
75 /// 执行带一个存储过程参数的的SQL语句。
76 /// </summary>
77 /// <param name="SQLString">SQL语句</param>
78 /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
79 /// <returns>影响的记录数</returns>
80 public static int ExecuteSql(string SQLString, string content)
81 {
82 using (OleDbConnection connection = new OleDbConnection(connectionString))
83 {
84 OleDbCommand cmd = new OleDbCommand(SQLString, connection);
85 System.Data.OleDb.OleDbParameter myParameter = new System.Data.OleDb.OleDbParameter("@content", OleDbType.VarChar);
86 myParameter.Value = content;
87 cmd.Parameters.Add(myParameter);
88 try
89 {
90 connection.Open();
91 int rows = cmd.ExecuteNonQuery();
92 return rows;
93 }
94 catch (System.Data.OleDb.OleDbException E)
95 {
96 throw new Exception(E.Message);
97 }
98 finally
99 {
100 cmd.Dispose();
101 connection.Close();
102 }
103 }
104 }
105 /**//// <summary>
106 /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
107 /// </summary>
108 /// <param name="strSQL">SQL语句</param>
109 /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
110 /// <returns>影响的记录数</returns>
111 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
112 {
113 using (OleDbConnection connection = new OleDbConnection(connectionString))
114 {
115 OleDbCommand cmd = new OleDbCommand(strSQL, connection);
116 System.Data.OleDb.OleDbParameter myParameter = new System.Data.OleDb.OleDbParameter("@fs", OleDbType.Binary);
117 myParameter.Value = fs;
118 cmd.Parameters.Add(myParameter);
119 try
120 {
121 connection.Open();
122 int rows = cmd.ExecuteNonQuery();
123 return rows;
124 }
125 catch (System.Data.OleDb.OleDbException E)
126 {
127 throw new Exception(E.Message);
128 }
129 finally
130 {
131 cmd.Dispose();
132 connection.Close();
133 }
134 }
135 }
136
137 /**//// <summary>
138 /// 执行一条计算查询结果语句,返回查询结果(object)。
139 /// </summary>
140 /// <param name="SQLString">计算查询结果语句</param>
141 /// <returns>查询结果(object)</returns>
142 public static object GetSingle(string SQLString)
143 {
144 using (OleDbConnection connection = new OleDbConnection(connectionString))
145 {
146 using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
147 {
148 try
149 {
150 connection.Open();
151 object obj = cmd.ExecuteScalar();
152 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
153 {
154 return null;
155 }
156 else
157 {
158 return obj;
159 }
160 }
161 catch (System.Data.OleDb.OleDbException e)
162 {
163 connection.Close();
164 throw new Exception(e.Message);
165 }
166 }
167 }
168 }
169 /**//// <summary>
170 /// 执行查询语句,返回OleDbDataReader
171 /// </summary>
172 /// <param name="strSQL">查询语句</param>
173 /// <returns>OleDbDataReader</returns>
174 public static OleDbDataReader ExecuteReader(string strSQL)
175 {
176 OleDbConnection connection = new OleDbConnection(connectionString);
177 OleDbCommand cmd = new OleDbCommand(strSQL, connection);
178 try
179 {
180 connection.Open();
181 OleDbDataReader myReader = cmd.ExecuteReader();
182 return myReader;
183 }
184 catch (System.Data.OleDb.OleDbException e)
185 {
186 throw new Exception(e.Message);
187 }
188
189 }
190 /**//// <summary>
191 /// 执行查询语句,返回DataSet
192 /// </summary>
193 /// <param name="SQLString">查询语句</param>
194 /// <returns>DataSet</returns>
195 public static DataSet Query(string SQLString)
196 {
197 using (OleDbConnection connection = new OleDbConnection(connectionString))
198 {
199 DataSet ds = new DataSet();
200 try
201 {
202 connection.Open();
203 OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
204 command.Fill(ds, "ds");
205 }
206 catch (System.Data.OleDb.OleDbException ex)
207 {
208 throw new Exception(ex.Message);
209 }
210 return ds;
211 }
212 }
213
214
215 #endregion
216
217 执行带参数的SQL语句#region 执行带参数的SQL语句
218
219 /**//// <summary>
220 /// 执行SQL语句,返回影响的记录数
221 /// </summary>
222 /// <param name="SQLString">SQL语句</param>
223 /// <returns>影响的记录数</returns>
224 public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
225 {
226 using (OleDbConnection connection = new OleDbConnection(connectionString))
227 {
228 using (OleDbCommand cmd = new OleDbCommand())
229 {
230 try
231 {
232 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
233 int rows = cmd.ExecuteNonQuery();
234 cmd.Parameters.Clear();
235 return rows;
236 }
237 catch (System.Data.OleDb.OleDbException E)
238 {
239 throw new Exception(E.Message);
240 }
241 }
242 }
243 }
244
245
246 /**//// <summary>
247 /// 执行多条SQL语句,实现数据库事务。
248 /// </summary>
249 /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的OleDbParameter[])</param>
250 public static void ExecuteSqlTran(Hashtable SQLStringList)
251 {
252 using (OleDbConnection conn = new OleDbConnection(connectionString))
253 {
254 conn.Open();
255 using (OleDbTransaction trans = conn.BeginTransaction())
256 {
257 OleDbCommand cmd = new OleDbCommand();
258 try
259 {
260 //循环
261 foreach (DictionaryEntry myDE in SQLStringList)
262 {
263 string cmdText = myDE.Key.ToString();
264 OleDbParameter[] cmdParms = (OleDbParameter[])myDE.Value;
265 PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
266 int val = cmd.ExecuteNonQuery();
267 cmd.Parameters.Clear();
268
269 trans.Commit();
270 }
271 }
272 catch
273 {
274 trans.Rollback();
275 throw;
276 }
277 }
278 }
279 }
280
281
282 /**//// <summary>
283 /// 执行一条计算查询结果语句,返回查询结果(object)。
284 /// </summary>
285 /// <param name="SQLString">计算查询结果语句</param>
286 /// <returns>查询结果(object)</returns>
287 public static object GetSingle(string SQLString, params OleDbParameter[] cmdParms)
288 {
289 using (OleDbConnection connection = new OleDbConnection(connectionString))
290 {
291 using (OleDbCommand cmd = new OleDbCommand())
292 {
293 try
294 {
295 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
296 object obj = cmd.ExecuteScalar();
297 cmd.Parameters.Clear();
298 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
299 {
300 return null;
301 }
302 else
303 {
304 return obj;
305 }
306 }
307 catch (System.Data.OleDb.OleDbException e)
308 {
309 throw new Exception(e.Message);
310 }
311 }
312 }
313 }
314
315 /**//// <summary>
316 /// 执行查询语句,返回OleDbDataReader
317 /// </summary>
318 /// <param name="strSQL">查询语句</param>
319 /// <returns>OleDbDataReader</returns>
320 public static OleDbDataReader ExecuteReader(string SQLString, params OleDbParameter[] cmdParms)
321 {
322 OleDbConnection connection = new OleDbConnection(connectionString);
323 OleDbCommand cmd = new OleDbCommand();
324 try
325 {
326 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
327 OleDbDataReader myReader = cmd.ExecuteReader();
328 cmd.Parameters.Clear();
329 return myReader;
330 }
331 catch (System.Data.OleDb.OleDbException e)
332 {
333 throw new Exception(e.Message);
334 }
335
336 }
337
338 /**//// <summary>
339 /// 执行查询语句,返回DataSet
340 /// </summary>
341 /// <param name="SQLString">查询语句</param>
342 /// <returns>DataSet</returns>
343 public static DataSet Query(string SQLString, params OleDbParameter[] cmdParms)
344 {
345 using (OleDbConnection connection = new OleDbConnection(connectionString))
346 {
347 OleDbCommand cmd = new OleDbCommand();
348 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
349 using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
350 {
351 DataSet ds = new DataSet();
352 try
353 {
354 da.Fill(ds, "ds");
355 cmd.Parameters.Clear();
356 }
357 catch (System.Data.OleDb.OleDbException ex)
358 {
359 throw new Exception(ex.Message);
360 }
361 return ds;
362 }
363 }
364 }
365
366
367 private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, OleDbParameter[] cmdParms)
368 {
369 if (conn.State != ConnectionState.Open)
370 conn.Open();
371 cmd.Connection = conn;
372 cmd.CommandText = cmdText;
373 if (trans != null)
374 cmd.Transaction = trans;
375 cmd.CommandType = CommandType.Text;//cmdType;
376 if (cmdParms != null)
377 {
378 foreach (OleDbParameter parm in cmdParms)
379 cmd.Parameters.Add(parm);
380 }
381 }
382
383 #endregion
384
385
386
387 }
2 /// 数据访问基础类(基于OleDb)
3 /// Copyright (C) 2004-2008 HOMEZZM
4 /// </summary>
5 public abstract class DbHelperOleDb
6 {
7 //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.
8 public static string connectionString = PubConstant.ConnectionString;
9 public DbHelperOleDb()
10 {
11 }
12
13 执行简单SQL语句#region 执行简单SQL语句
14
15 /**//// <summary>
16 /// 执行SQL语句,返回影响的记录数
17 /// </summary>
18 /// <param name="SQLString">SQL语句</param>
19 /// <returns>影响的记录数</returns>
20 public static int ExecuteSql(string SQLString)
21 {
22 using (OleDbConnection connection = new OleDbConnection(connectionString))
23 {
24 using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
25 {
26 try
27 {
28 connection.Open();
29 int rows = cmd.ExecuteNonQuery();
30 return rows;
31 }
32 catch (System.Data.OleDb.OleDbException E)
33 {
34 connection.Close();
35 throw new Exception(E.Message);
36 }
37 }
38 }
39 }
40
41 /**//// <summary>
42 /// 执行多条SQL语句,实现数据库事务。
43 /// </summary>
44 /// <param name="SQLStringList">多条SQL语句</param>
45 public static void ExecuteSqlTran(ArrayList SQLStringList)
46 {
47 using (OleDbConnection conn = new OleDbConnection(connectionString))
48 {
49 conn.Open();
50 OleDbCommand cmd = new OleDbCommand();
51 cmd.Connection = conn;
52 OleDbTransaction tx = conn.BeginTransaction();
53 cmd.Transaction = tx;
54 try
55 {
56 for (int n = 0; n < SQLStringList.Count; n++)
57 {
58 string strsql = SQLStringList[n].ToString();
59 if (strsql.Trim().Length > 1)
60 {
61 cmd.CommandText = strsql;
62 cmd.ExecuteNonQuery();
63 }
64 }
65 tx.Commit();
66 }
67 catch (System.Data.OleDb.OleDbException E)
68 {
69 tx.Rollback();
70 throw new Exception(E.Message);
71 }
72 }
73 }
74 /**//// <summary>
75 /// 执行带一个存储过程参数的的SQL语句。
76 /// </summary>
77 /// <param name="SQLString">SQL语句</param>
78 /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
79 /// <returns>影响的记录数</returns>
80 public static int ExecuteSql(string SQLString, string content)
81 {
82 using (OleDbConnection connection = new OleDbConnection(connectionString))
83 {
84 OleDbCommand cmd = new OleDbCommand(SQLString, connection);
85 System.Data.OleDb.OleDbParameter myParameter = new System.Data.OleDb.OleDbParameter("@content", OleDbType.VarChar);
86 myParameter.Value = content;
87 cmd.Parameters.Add(myParameter);
88 try
89 {
90 connection.Open();
91 int rows = cmd.ExecuteNonQuery();
92 return rows;
93 }
94 catch (System.Data.OleDb.OleDbException E)
95 {
96 throw new Exception(E.Message);
97 }
98 finally
99 {
100 cmd.Dispose();
101 connection.Close();
102 }
103 }
104 }
105 /**//// <summary>
106 /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
107 /// </summary>
108 /// <param name="strSQL">SQL语句</param>
109 /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
110 /// <returns>影响的记录数</returns>
111 public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
112 {
113 using (OleDbConnection connection = new OleDbConnection(connectionString))
114 {
115 OleDbCommand cmd = new OleDbCommand(strSQL, connection);
116 System.Data.OleDb.OleDbParameter myParameter = new System.Data.OleDb.OleDbParameter("@fs", OleDbType.Binary);
117 myParameter.Value = fs;
118 cmd.Parameters.Add(myParameter);
119 try
120 {
121 connection.Open();
122 int rows = cmd.ExecuteNonQuery();
123 return rows;
124 }
125 catch (System.Data.OleDb.OleDbException E)
126 {
127 throw new Exception(E.Message);
128 }
129 finally
130 {
131 cmd.Dispose();
132 connection.Close();
133 }
134 }
135 }
136
137 /**//// <summary>
138 /// 执行一条计算查询结果语句,返回查询结果(object)。
139 /// </summary>
140 /// <param name="SQLString">计算查询结果语句</param>
141 /// <returns>查询结果(object)</returns>
142 public static object GetSingle(string SQLString)
143 {
144 using (OleDbConnection connection = new OleDbConnection(connectionString))
145 {
146 using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
147 {
148 try
149 {
150 connection.Open();
151 object obj = cmd.ExecuteScalar();
152 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
153 {
154 return null;
155 }
156 else
157 {
158 return obj;
159 }
160 }
161 catch (System.Data.OleDb.OleDbException e)
162 {
163 connection.Close();
164 throw new Exception(e.Message);
165 }
166 }
167 }
168 }
169 /**//// <summary>
170 /// 执行查询语句,返回OleDbDataReader
171 /// </summary>
172 /// <param name="strSQL">查询语句</param>
173 /// <returns>OleDbDataReader</returns>
174 public static OleDbDataReader ExecuteReader(string strSQL)
175 {
176 OleDbConnection connection = new OleDbConnection(connectionString);
177 OleDbCommand cmd = new OleDbCommand(strSQL, connection);
178 try
179 {
180 connection.Open();
181 OleDbDataReader myReader = cmd.ExecuteReader();
182 return myReader;
183 }
184 catch (System.Data.OleDb.OleDbException e)
185 {
186 throw new Exception(e.Message);
187 }
188
189 }
190 /**//// <summary>
191 /// 执行查询语句,返回DataSet
192 /// </summary>
193 /// <param name="SQLString">查询语句</param>
194 /// <returns>DataSet</returns>
195 public static DataSet Query(string SQLString)
196 {
197 using (OleDbConnection connection = new OleDbConnection(connectionString))
198 {
199 DataSet ds = new DataSet();
200 try
201 {
202 connection.Open();
203 OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
204 command.Fill(ds, "ds");
205 }
206 catch (System.Data.OleDb.OleDbException ex)
207 {
208 throw new Exception(ex.Message);
209 }
210 return ds;
211 }
212 }
213
214
215 #endregion
216
217 执行带参数的SQL语句#region 执行带参数的SQL语句
218
219 /**//// <summary>
220 /// 执行SQL语句,返回影响的记录数
221 /// </summary>
222 /// <param name="SQLString">SQL语句</param>
223 /// <returns>影响的记录数</returns>
224 public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
225 {
226 using (OleDbConnection connection = new OleDbConnection(connectionString))
227 {
228 using (OleDbCommand cmd = new OleDbCommand())
229 {
230 try
231 {
232 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
233 int rows = cmd.ExecuteNonQuery();
234 cmd.Parameters.Clear();
235 return rows;
236 }
237 catch (System.Data.OleDb.OleDbException E)
238 {
239 throw new Exception(E.Message);
240 }
241 }
242 }
243 }
244
245
246 /**//// <summary>
247 /// 执行多条SQL语句,实现数据库事务。
248 /// </summary>
249 /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的OleDbParameter[])</param>
250 public static void ExecuteSqlTran(Hashtable SQLStringList)
251 {
252 using (OleDbConnection conn = new OleDbConnection(connectionString))
253 {
254 conn.Open();
255 using (OleDbTransaction trans = conn.BeginTransaction())
256 {
257 OleDbCommand cmd = new OleDbCommand();
258 try
259 {
260 //循环
261 foreach (DictionaryEntry myDE in SQLStringList)
262 {
263 string cmdText = myDE.Key.ToString();
264 OleDbParameter[] cmdParms = (OleDbParameter[])myDE.Value;
265 PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
266 int val = cmd.ExecuteNonQuery();
267 cmd.Parameters.Clear();
268
269 trans.Commit();
270 }
271 }
272 catch
273 {
274 trans.Rollback();
275 throw;
276 }
277 }
278 }
279 }
280
281
282 /**//// <summary>
283 /// 执行一条计算查询结果语句,返回查询结果(object)。
284 /// </summary>
285 /// <param name="SQLString">计算查询结果语句</param>
286 /// <returns>查询结果(object)</returns>
287 public static object GetSingle(string SQLString, params OleDbParameter[] cmdParms)
288 {
289 using (OleDbConnection connection = new OleDbConnection(connectionString))
290 {
291 using (OleDbCommand cmd = new OleDbCommand())
292 {
293 try
294 {
295 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
296 object obj = cmd.ExecuteScalar();
297 cmd.Parameters.Clear();
298 if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
299 {
300 return null;
301 }
302 else
303 {
304 return obj;
305 }
306 }
307 catch (System.Data.OleDb.OleDbException e)
308 {
309 throw new Exception(e.Message);
310 }
311 }
312 }
313 }
314
315 /**//// <summary>
316 /// 执行查询语句,返回OleDbDataReader
317 /// </summary>
318 /// <param name="strSQL">查询语句</param>
319 /// <returns>OleDbDataReader</returns>
320 public static OleDbDataReader ExecuteReader(string SQLString, params OleDbParameter[] cmdParms)
321 {
322 OleDbConnection connection = new OleDbConnection(connectionString);
323 OleDbCommand cmd = new OleDbCommand();
324 try
325 {
326 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
327 OleDbDataReader myReader = cmd.ExecuteReader();
328 cmd.Parameters.Clear();
329 return myReader;
330 }
331 catch (System.Data.OleDb.OleDbException e)
332 {
333 throw new Exception(e.Message);
334 }
335
336 }
337
338 /**//// <summary>
339 /// 执行查询语句,返回DataSet
340 /// </summary>
341 /// <param name="SQLString">查询语句</param>
342 /// <returns>DataSet</returns>
343 public static DataSet Query(string SQLString, params OleDbParameter[] cmdParms)
344 {
345 using (OleDbConnection connection = new OleDbConnection(connectionString))
346 {
347 OleDbCommand cmd = new OleDbCommand();
348 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
349 using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
350 {
351 DataSet ds = new DataSet();
352 try
353 {
354 da.Fill(ds, "ds");
355 cmd.Parameters.Clear();
356 }
357 catch (System.Data.OleDb.OleDbException ex)
358 {
359 throw new Exception(ex.Message);
360 }
361 return ds;
362 }
363 }
364 }
365
366
367 private static void PrepareCommand(OleDbCommand cmd, OleDbConnection conn, OleDbTransaction trans, string cmdText, OleDbParameter[] cmdParms)
368 {
369 if (conn.State != ConnectionState.Open)
370 conn.Open();
371 cmd.Connection = conn;
372 cmd.CommandText = cmdText;
373 if (trans != null)
374 cmd.Transaction = trans;
375 cmd.CommandType = CommandType.Text;//cmdType;
376 if (cmdParms != null)
377 {
378 foreach (OleDbParameter parm in cmdParms)
379 cmd.Parameters.Add(parm);
380 }
381 }
382
383 #endregion
384
385
386
387 }