偶得一个绝佳C#数据库封装类,与大家分享一下! -----抄楚广明老师的

1 using  System;
  2 using  System.Collections.Generic;
  3 using  System.Text;
  4 using  System.Data;
  5 using  System.Configuration;
  6 using  System.Data.Common;
  7 using  System.Data.SqlClient;
  8 using  System.Data.OleDb;
  9 using  System.Data.Odbc;
 10 using  System.Data.OracleClient;
 11 using  System.IO;
 12
 13 namespace  BinaryIntellect.DataAccess
 14 ExpandedBlockStart.gifContractedBlock.gif {
 15    public class DatabaseHelper:IDisposable
 16ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 17        private string strConnectionString;
 18        private DbConnection objConnection;
 19        private DbCommand objCommand;
 20        private DbProviderFactory objFactory = null;
 21        private bool boolHandleErrors;
 22        private string strLastError;
 23        private bool boolLogError;
 24        private string strLogFile;
 25
 26        public DatabaseHelper(string connectionstring,Providers provider)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 28            strConnectionString = connectionstring;
 29            switch (provider)
 30ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 31                case Providers.SqlServer:
 32                    objFactory = SqlClientFactory.Instance;
 33                    break;
 34                case Providers.OleDb:
 35                    objFactory = OleDbFactory.Instance;
 36                    break;
 37                case Providers.Oracle:
 38                    objFactory = OracleClientFactory.Instance;
 39                    break;
 40                case Providers.ODBC:
 41                    objFactory = OdbcFactory.Instance;
 42                    break;
 43                case Providers.ConfigDefined:
 44                    string providername=ConfigurationManager.ConnectionStrings["connectionstring"].ProviderName;
 45                    switch (providername)
 46ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 47                        case "System.Data.SqlClient":
 48                            objFactory = SqlClientFactory.Instance;
 49                            break;
 50                        case "System.Data.OleDb":
 51                            objFactory = OleDbFactory.Instance;
 52                            break;
 53                        case "System.Data.OracleClient":
 54                            objFactory = OracleClientFactory.Instance;
 55                            break;
 56                        case "System.Data.Odbc":
 57                            objFactory = OdbcFactory.Instance;
 58                            break;
 59                    }

 60                    break;
 61
 62            }

 63            objConnection = objFactory.CreateConnection();
 64            objCommand = objFactory.CreateCommand();
 65
 66            objConnection.ConnectionString = strConnectionString;
 67            objCommand.Connection = objConnection;
 68        }

 69
 70        public DatabaseHelper(Providers provider):this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString,provider)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 72        }

 73
 74        public DatabaseHelper(string connectionstring): this(connectionstring, Providers.SqlServer)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 76        }

 77
 78        public DatabaseHelper():this(ConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString,Providers.ConfigDefined)
 79ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 80        }

 81
 82        public bool HandleErrors
 83ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 84            get
 85ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 86                return boolHandleErrors;
 87            }

 88            set
 89ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 90                boolHandleErrors = value;
 91            }

 92        }

 93
 94        public string LastError
 95ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 96            get
 97ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 98                return strLastError;
 99            }

100        }

101
102        public bool LogErrors
103ExpandedSubBlockStart.gifContractedSubBlock.gif        {
104            get
105ExpandedSubBlockStart.gifContractedSubBlock.gif            {
106                return boolLogError;
107            }

108            set
109ExpandedSubBlockStart.gifContractedSubBlock.gif            {
110                boolLogError=value;
111            }

112        }

113
114        public string LogFile
115ExpandedSubBlockStart.gifContractedSubBlock.gif        {
116            get
117ExpandedSubBlockStart.gifContractedSubBlock.gif            {
118                return strLogFile;
119            }

120            set
121ExpandedSubBlockStart.gifContractedSubBlock.gif            {
122                strLogFile = value;
123            }

124        }

125
126        public int AddParameter(string name,object value)
127ExpandedSubBlockStart.gifContractedSubBlock.gif        {
128            DbParameter p = objFactory.CreateParameter();
129            p.ParameterName = name;
130            p.Value=value;
131            return objCommand.Parameters.Add(p);
132        }

133
134        public int AddParameter(DbParameter parameter)
135ExpandedSubBlockStart.gifContractedSubBlock.gif        {
136            return objCommand.Parameters.Add(parameter);
137        }

138
139        public DbCommand Command
140ExpandedSubBlockStart.gifContractedSubBlock.gif        {
141            get
142ExpandedSubBlockStart.gifContractedSubBlock.gif            {
143                return objCommand;
144            }

145        }

146
147        public void BeginTransaction()
148ExpandedSubBlockStart.gifContractedSubBlock.gif        {
149            if (objConnection.State == System.Data.ConnectionState.Closed)
150ExpandedSubBlockStart.gifContractedSubBlock.gif            {
151                objConnection.Open();
152            }

153            objCommand.Transaction = objConnection.BeginTransaction();
154        }

155
156        public void CommitTransaction()
157ExpandedSubBlockStart.gifContractedSubBlock.gif        {
158            objCommand.Transaction.Commit();
159            objConnection.Close();
160        }

161
162        public void RollbackTransaction()
163ExpandedSubBlockStart.gifContractedSubBlock.gif        {
164            objCommand.Transaction.Rollback();
165            objConnection.Close();
166        }

167
168        public int ExecuteNonQuery(string query)
169ExpandedSubBlockStart.gifContractedSubBlock.gif        {
170            return ExecuteNonQuery(query, CommandType.Text, ConnectionState.CloseOnExit);
171        }

172
173        public int ExecuteNonQuery(string query,CommandType commandtype)
174ExpandedSubBlockStart.gifContractedSubBlock.gif        {
175            return ExecuteNonQuery(query, commandtype, ConnectionState.CloseOnExit);
176        }

177
178        public int ExecuteNonQuery(string query,ConnectionState connectionstate)
179ExpandedSubBlockStart.gifContractedSubBlock.gif        {
180            return ExecuteNonQuery(query,CommandType.Text,connectionstate);
181        }

182
183        public int ExecuteNonQuery(string query,CommandType commandtype, ConnectionState connectionstate)
184ExpandedSubBlockStart.gifContractedSubBlock.gif        {
185            objCommand.CommandText = query;
186            objCommand.CommandType = commandtype;
187            int i=-1;
188            try
189ExpandedSubBlockStart.gifContractedSubBlock.gif            {
190                if (objConnection.State == System.Data.ConnectionState.Closed)
191ExpandedSubBlockStart.gifContractedSubBlock.gif                {
192                    objConnection.Open();
193                }

194                i = objCommand.ExecuteNonQuery();
195            }

196            catch (Exception ex)
197ExpandedSubBlockStart.gifContractedSubBlock.gif            {
198                HandleExceptions(ex);
199            }

200            finally
201ExpandedSubBlockStart.gifContractedSubBlock.gif            {
202                objCommand.Parameters.Clear();
203                if (connectionstate == ConnectionState.CloseOnExit)
204ExpandedSubBlockStart.gifContractedSubBlock.gif                {
205                    objConnection.Close();
206                }

207            }

208
209            return i;
210        }

211
212        public object ExecuteScalar(string query)
213ExpandedSubBlockStart.gifContractedSubBlock.gif        {
214            return ExecuteScalar(query, CommandType.Text, ConnectionState.CloseOnExit);
215        }

216
217        public object ExecuteScalar(string query,CommandType commandtype)
218ExpandedSubBlockStart.gifContractedSubBlock.gif        {
219            return ExecuteScalar(query, commandtype, ConnectionState.CloseOnExit);
220        }

221
222        public object ExecuteScalar(string query, ConnectionState connectionstate)
223ExpandedSubBlockStart.gifContractedSubBlock.gif        {
224            return ExecuteScalar(query, CommandType.Text, connectionstate);
225        }

226
227        public object ExecuteScalar(string query,CommandType commandtype, ConnectionState connectionstate)
228ExpandedSubBlockStart.gifContractedSubBlock.gif        {
229            objCommand.CommandText = query;
230            objCommand.CommandType = commandtype;
231            object o = null;
232            try
233ExpandedSubBlockStart.gifContractedSubBlock.gif            {
234                if (objConnection.State == System.Data.ConnectionState.Closed)
235ExpandedSubBlockStart.gifContractedSubBlock.gif                {
236                    objConnection.Open();
237                }

238                o = objCommand.ExecuteScalar();
239            }

240            catch (Exception ex)
241ExpandedSubBlockStart.gifContractedSubBlock.gif            {
242                HandleExceptions(ex);
243            }

244            finally
245ExpandedSubBlockStart.gifContractedSubBlock.gif            {
246                objCommand.Parameters.Clear();
247                if (connectionstate == ConnectionState.CloseOnExit)
248ExpandedSubBlockStart.gifContractedSubBlock.gif                {
249                    objConnection.Close();
250                }

251            }

252
253            return o;
254        }

255
256        public DbDataReader ExecuteReader(string query)
257ExpandedSubBlockStart.gifContractedSubBlock.gif        {
258            return ExecuteReader(query, CommandType.Text, ConnectionState.CloseOnExit);
259        }

260
261        public DbDataReader ExecuteReader(string query,CommandType commandtype)
262ExpandedSubBlockStart.gifContractedSubBlock.gif        {
263            return ExecuteReader(query, commandtype, ConnectionState.CloseOnExit);
264        }

265
266        public DbDataReader ExecuteReader(string query, ConnectionState connectionstate)
267ExpandedSubBlockStart.gifContractedSubBlock.gif        {
268            return ExecuteReader(query, CommandType.Text, connectionstate);
269        }

270
271        public DbDataReader ExecuteReader(string query,CommandType commandtype, ConnectionState connectionstate)
272ExpandedSubBlockStart.gifContractedSubBlock.gif        {
273            objCommand.CommandText = query;
274            objCommand.CommandType = commandtype;
275            DbDataReader reader=null;
276            try
277ExpandedSubBlockStart.gifContractedSubBlock.gif            {
278                if (objConnection.State == System.Data.ConnectionState.Closed)
279ExpandedSubBlockStart.gifContractedSubBlock.gif                {
280                    objConnection.Open();
281                }

282                if (connectionstate == ConnectionState.CloseOnExit)
283ExpandedSubBlockStart.gifContractedSubBlock.gif                {
284                    reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection);
285                }

286                else
287ExpandedSubBlockStart.gifContractedSubBlock.gif                {
288                    reader = objCommand.ExecuteReader();
289                }

290
291            }

292            catch (Exception ex)
293ExpandedSubBlockStart.gifContractedSubBlock.gif            {
294                HandleExceptions(ex);
295            }

296            finally
297ExpandedSubBlockStart.gifContractedSubBlock.gif            {
298                objCommand.Parameters.Clear();
299            }

300
301            return reader;
302        }

303
304        public DataSet ExecuteDataSet(string query)
305ExpandedSubBlockStart.gifContractedSubBlock.gif        {
306            return ExecuteDataSet(query, CommandType.Text, ConnectionState.CloseOnExit);
307        }

308
309        public DataSet ExecuteDataSet(string query,CommandType commandtype)
310ExpandedSubBlockStart.gifContractedSubBlock.gif        {
311            return ExecuteDataSet(query, commandtype, ConnectionState.CloseOnExit);
312        }

313
314        public DataSet ExecuteDataSet(string query,ConnectionState connectionstate)
315ExpandedSubBlockStart.gifContractedSubBlock.gif        {
316            return ExecuteDataSet(query, CommandType.Text, connectionstate);
317        }

318
319        public DataSet ExecuteDataSet(string query,CommandType commandtype, ConnectionState connectionstate)
320ExpandedSubBlockStart.gifContractedSubBlock.gif        {
321            DbDataAdapter adapter = objFactory.CreateDataAdapter();
322            objCommand.CommandText = query;
323            objCommand.CommandType = commandtype;
324            adapter.SelectCommand = objCommand;
325            DataSet ds = new DataSet();
326            try
327ExpandedSubBlockStart.gifContractedSubBlock.gif            {
328                adapter.Fill(ds);
329            }

330            catch (Exception ex)
331ExpandedSubBlockStart.gifContractedSubBlock.gif            {
332                HandleExceptions(ex);
333            }

334            finally
335ExpandedSubBlockStart.gifContractedSubBlock.gif            {
336                objCommand.Parameters.Clear();
337                if (connectionstate == ConnectionState.CloseOnExit)
338ExpandedSubBlockStart.gifContractedSubBlock.gif                {
339                    if (objConnection.State == System.Data.ConnectionState.Open)
340ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
341                        objConnection.Close();
342                    }

343                }

344            }

345            return ds;
346        }

347
348        private void HandleExceptions(Exception ex)
349ExpandedSubBlockStart.gifContractedSubBlock.gif        {
350            if (LogErrors)
351ExpandedSubBlockStart.gifContractedSubBlock.gif            {
352                WriteToLog(ex.Message);
353            }

354            if (HandleErrors)
355ExpandedSubBlockStart.gifContractedSubBlock.gif            {
356                strLastError = ex.Message;
357            }

358            else
359ExpandedSubBlockStart.gifContractedSubBlock.gif            {
360                throw ex;
361            }

362        }

363
364        private void WriteToLog(string msg)
365ExpandedSubBlockStart.gifContractedSubBlock.gif        {
366            StreamWriter writer= File.AppendText(LogFile);
367            writer.WriteLine(DateTime.Now.ToString() + " - " + msg);
368            writer.Close();
369        }

370        
371        public void Dispose()
372ExpandedSubBlockStart.gifContractedSubBlock.gif        {
373            objConnection.Close();
374            objConnection.Dispose();
375            objCommand.Dispose();
376        }

377
378    }

379
380    public enum Providers
381ExpandedSubBlockStart.gifContractedSubBlock.gif    {
382        SqlServer,OleDb,Oracle,ODBC,ConfigDefined
383    }

384
385    public enum ConnectionState
386ExpandedSubBlockStart.gifContractedSubBlock.gif    {
387        KeepOpen,CloseOnExit
388    }

389}

390
391

转载于:https://www.cnblogs.com/netkey/articles/1311258.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值