NET-数据访问基础类(基于OleDb)

代码
  1 ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
  2    /// 数据访问基础类(基于OleDb) 
  3    /// Copyright (C) 2004-2008 HOMEZZM
  4    /// </summary>

  5      public   abstract   class  DbHelperOleDb
  6 ExpandedBlockStart.gifContractedBlock.gif     {
  7        //数据库连接字符串(web.config来配置),可以动态更改connectionString支持多数据库.        
  8        public static string connectionString = PubConstant.ConnectionString;             
  9        public DbHelperOleDb()
 10ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 11        }

 12
 13ContractedSubBlock.gifExpandedSubBlockStart.gif        执行简单SQL语句#region  执行简单SQL语句
 14
 15ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 16        /// 执行SQL语句,返回影响的记录数
 17        /// </summary>
 18        /// <param name="SQLString">SQL语句</param>
 19        /// <returns>影响的记录数</returns>

 20        public static int ExecuteSql(string SQLString)
 21ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 22            using (OleDbConnection connection = new OleDbConnection(connectionString))
 23ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 24                using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
 25ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 26                    try
 27ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 28                        connection.Open();
 29                        int rows = cmd.ExecuteNonQuery();
 30                        return rows;
 31                    }

 32                    catch (System.Data.OleDb.OleDbException E)
 33ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 34                        connection.Close();
 35                        throw new Exception(E.Message);
 36                    }

 37                }

 38            }

 39        }

 40
 41ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
 42        /// 执行多条SQL语句,实现数据库事务。
 43        /// </summary>
 44        /// <param name="SQLStringList">多条SQL语句</param>        

 45        public static void ExecuteSqlTran(ArrayList SQLStringList)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 47            using (OleDbConnection conn = new OleDbConnection(connectionString))
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 49                conn.Open();
 50                OleDbCommand cmd = new OleDbCommand();
 51                cmd.Connection = conn;
 52                OleDbTransaction tx = conn.BeginTransaction();
 53                cmd.Transaction = tx;
 54                try
 55ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 56                    for (int n = 0; n < SQLStringList.Count; n++)
 57ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 58                        string strsql = SQLStringList[n].ToString();
 59                        if (strsql.Trim().Length > 1)
 60ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
 61                            cmd.CommandText = strsql;
 62                            cmd.ExecuteNonQuery();
 63                        }

 64                    }

 65                    tx.Commit();
 66                }

 67                catch (System.Data.OleDb.OleDbException E)
 68ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 69                    tx.Rollback();
 70                    throw new Exception(E.Message);
 71                }

 72            }

 73        }

 74ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
 81ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 82            using (OleDbConnection connection = new OleDbConnection(connectionString))
 83ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 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
 89ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 90                    connection.Open();
 91                    int rows = cmd.ExecuteNonQuery();
 92                    return rows;
 93                }

 94                catch (System.Data.OleDb.OleDbException E)
 95ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 96                    throw new Exception(E.Message);
 97                }

 98                finally
 99ExpandedSubBlockStart.gifContractedSubBlock.gif                {
100                    cmd.Dispose();
101                    connection.Close();
102                }

103            }

104        }

105ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <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)
112ExpandedSubBlockStart.gifContractedSubBlock.gif        {
113            using (OleDbConnection connection = new OleDbConnection(connectionString))
114ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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
120ExpandedSubBlockStart.gifContractedSubBlock.gif                {
121                    connection.Open();
122                    int rows = cmd.ExecuteNonQuery();
123                    return rows;
124                }

125                catch (System.Data.OleDb.OleDbException E)
126ExpandedSubBlockStart.gifContractedSubBlock.gif                {
127                    throw new Exception(E.Message);
128                }

129                finally
130ExpandedSubBlockStart.gifContractedSubBlock.gif                {
131                    cmd.Dispose();
132                    connection.Close();
133                }

134            }

135        }

136
137ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
138        /// 执行一条计算查询结果语句,返回查询结果(object)。
139        /// </summary>
140        /// <param name="SQLString">计算查询结果语句</param>
141        /// <returns>查询结果(object)</returns>

142        public static object GetSingle(string SQLString)
143ExpandedSubBlockStart.gifContractedSubBlock.gif        {
144            using (OleDbConnection connection = new OleDbConnection(connectionString))
145ExpandedSubBlockStart.gifContractedSubBlock.gif            {
146                using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
147ExpandedSubBlockStart.gifContractedSubBlock.gif                {
148                    try
149ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
150                        connection.Open();
151                        object obj = cmd.ExecuteScalar();
152                        if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
153ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
154                            return null;
155                        }

156                        else
157ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
158                            return obj;
159                        }

160                    }

161                    catch (System.Data.OleDb.OleDbException e)
162ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
163                        connection.Close();
164                        throw new Exception(e.Message);
165                    }

166                }

167            }

168        }

169ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
170        /// 执行查询语句,返回OleDbDataReader
171        /// </summary>
172        /// <param name="strSQL">查询语句</param>
173        /// <returns>OleDbDataReader</returns>

174        public static OleDbDataReader ExecuteReader(string strSQL)
175ExpandedSubBlockStart.gifContractedSubBlock.gif        {
176            OleDbConnection connection = new OleDbConnection(connectionString);
177            OleDbCommand cmd = new OleDbCommand(strSQL, connection);
178            try
179ExpandedSubBlockStart.gifContractedSubBlock.gif            {
180                connection.Open();
181                OleDbDataReader myReader = cmd.ExecuteReader();
182                return myReader;
183            }

184            catch (System.Data.OleDb.OleDbException e)
185ExpandedSubBlockStart.gifContractedSubBlock.gif            {
186                throw new Exception(e.Message);
187            }

188
189        }

190ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
191        /// 执行查询语句,返回DataSet
192        /// </summary>
193        /// <param name="SQLString">查询语句</param>
194        /// <returns>DataSet</returns>

195        public static DataSet Query(string SQLString)
196ExpandedSubBlockStart.gifContractedSubBlock.gif        {
197            using (OleDbConnection connection = new OleDbConnection(connectionString))
198ExpandedSubBlockStart.gifContractedSubBlock.gif            {
199                DataSet ds = new DataSet();
200                try
201ExpandedSubBlockStart.gifContractedSubBlock.gif                {
202                    connection.Open();
203                    OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
204                    command.Fill(ds, "ds");
205                }

206                catch (System.Data.OleDb.OleDbException ex)
207ExpandedSubBlockStart.gifContractedSubBlock.gif                {
208                    throw new Exception(ex.Message);
209                }

210                return ds;
211            }

212        }

213
214
215        #endregion

216
217ContractedSubBlock.gifExpandedSubBlockStart.gif        执行带参数的SQL语句#region 执行带参数的SQL语句
218
219ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
220        /// 执行SQL语句,返回影响的记录数
221        /// </summary>
222        /// <param name="SQLString">SQL语句</param>
223        /// <returns>影响的记录数</returns>

224        public static int ExecuteSql(string SQLString, params OleDbParameter[] cmdParms)
225ExpandedSubBlockStart.gifContractedSubBlock.gif        {
226            using (OleDbConnection connection = new OleDbConnection(connectionString))
227ExpandedSubBlockStart.gifContractedSubBlock.gif            {
228                using (OleDbCommand cmd = new OleDbCommand())
229ExpandedSubBlockStart.gifContractedSubBlock.gif                {
230                    try
231ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
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)
238ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
239                        throw new Exception(E.Message);
240                    }

241                }

242            }

243        }

244
245
246ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
247        /// 执行多条SQL语句,实现数据库事务。
248        /// </summary>
249        /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的OleDbParameter[])</param>

250        public static void ExecuteSqlTran(Hashtable SQLStringList)
251ExpandedSubBlockStart.gifContractedSubBlock.gif        {
252            using (OleDbConnection conn = new OleDbConnection(connectionString))
253ExpandedSubBlockStart.gifContractedSubBlock.gif            {
254                conn.Open();
255                using (OleDbTransaction trans = conn.BeginTransaction())
256ExpandedSubBlockStart.gifContractedSubBlock.gif                {
257                    OleDbCommand cmd = new OleDbCommand();
258                    try
259ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
260                        //循环
261                        foreach (DictionaryEntry myDE in SQLStringList)
262ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
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
273ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
274                        trans.Rollback();
275                        throw;
276                    }

277                }

278            }

279        }

280
281
282ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
283        /// 执行一条计算查询结果语句,返回查询结果(object)。
284        /// </summary>
285        /// <param name="SQLString">计算查询结果语句</param>
286        /// <returns>查询结果(object)</returns>

287        public static object GetSingle(string SQLString, params OleDbParameter[] cmdParms)
288ExpandedSubBlockStart.gifContractedSubBlock.gif        {
289            using (OleDbConnection connection = new OleDbConnection(connectionString))
290ExpandedSubBlockStart.gifContractedSubBlock.gif            {
291                using (OleDbCommand cmd = new OleDbCommand())
292ExpandedSubBlockStart.gifContractedSubBlock.gif                {
293                    try
294ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
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)))
299ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
300                            return null;
301                        }

302                        else
303ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
304                            return obj;
305                        }

306                    }

307                    catch (System.Data.OleDb.OleDbException e)
308ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
309                        throw new Exception(e.Message);
310                    }

311                }

312            }

313        }

314
315ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
316        /// 执行查询语句,返回OleDbDataReader
317        /// </summary>
318        /// <param name="strSQL">查询语句</param>
319        /// <returns>OleDbDataReader</returns>

320        public static OleDbDataReader ExecuteReader(string SQLString, params OleDbParameter[] cmdParms)
321ExpandedSubBlockStart.gifContractedSubBlock.gif        {
322            OleDbConnection connection = new OleDbConnection(connectionString);
323            OleDbCommand cmd = new OleDbCommand();
324            try
325ExpandedSubBlockStart.gifContractedSubBlock.gif            {
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)
332ExpandedSubBlockStart.gifContractedSubBlock.gif            {
333                throw new Exception(e.Message);
334            }

335
336        }

337
338ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
339        /// 执行查询语句,返回DataSet
340        /// </summary>
341        /// <param name="SQLString">查询语句</param>
342        /// <returns>DataSet</returns>

343        public static DataSet Query(string SQLString, params OleDbParameter[] cmdParms)
344ExpandedSubBlockStart.gifContractedSubBlock.gif        {
345            using (OleDbConnection connection = new OleDbConnection(connectionString))
346ExpandedSubBlockStart.gifContractedSubBlock.gif            {
347                OleDbCommand cmd = new OleDbCommand();
348                PrepareCommand(cmd, connection, null, SQLString, cmdParms);
349                using (OleDbDataAdapter da = new OleDbDataAdapter(cmd))
350ExpandedSubBlockStart.gifContractedSubBlock.gif                {
351                    DataSet ds = new DataSet();
352                    try
353ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
354                        da.Fill(ds, "ds");
355                        cmd.Parameters.Clear();
356                    }

357                    catch (System.Data.OleDb.OleDbException ex)
358ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
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)
368ExpandedSubBlockStart.gifContractedSubBlock.gif        {
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)
377ExpandedSubBlockStart.gifContractedSubBlock.gif            {
378                foreach (OleDbParameter parm in cmdParms)
379                    cmd.Parameters.Add(parm);
380            }

381        }

382
383        #endregion

384
385    
386
387    }

转载于:https://www.cnblogs.com/homezzm/archive/2009/11/27/1612079.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值