也谈OleDbHelper,由SqlHelper类转OleDbHelper类,access操作

今意外看见一贴子谈OleDbHelper,突然想起自已一直用的由SqlHelper类转成的OleDbHelper,在此分享,自己用了二年了,还没发现异常, 代码如下:
  1. usingSystem;
  2. usingSystem.Data;
  3. usingSystem.Data.OleDb;
  4. usingSystem.Configuration;
  5. usingSystem.Collections;
  6. usingSystem.Data.Sql;
  7. usingSystem.Text;
  8. namespaceLihui.Common
  9. {
  10. ///<summary>
  11. ///SummarydescriptionforOleDbHelper
  12. ///</summary>
  13. publicclassOleDbHelper
  14. {
  15. //Databaseconnectionstrings
  16. publicstaticreadonlystringCONN_STRING=ConfigurationManager.AppSettings["OleDbConnectionString"];
  17. publicstaticreadonlystringCONN_STRING1=ConfigurationManager.AppSettings["OleDbConnectionString1"];
  18. //Hashtabletostorecachedparameters
  19. privatestaticHashtableparmCache=Hashtable.Synchronized(newHashtable());
  20. #region=ExecuteNonQuery=
  21. publicstaticintExecuteNonQuery(stringconnString,CommandTypecmdType,stringcmdText)
  22. {
  23. returnExecuteNonQuery(connString,cmdType,cmdText,null);
  24. }
  25. publicstaticintExecuteNonQuery(OleDbConnectionconn,CommandTypecmdType,stringcmdText)
  26. {
  27. returnExecuteNonQuery(conn,cmdType,cmdText,null);
  28. }
  29. publicstaticintExecuteNonQuery(OleDbTransactiontrans,CommandTypecmdType,stringcmdText)
  30. {
  31. returnExecuteNonQuery(trans,cmdType,cmdText,null);
  32. }
  33. publicstaticintExecuteNonQuery(stringconnString,CommandTypecmdType,stringcmdText,paramsOleDbParameter[]cmdParms)
  34. {
  35. OleDbCommandcmd=newOleDbCommand();
  36. using(OleDbConnectionconn=newOleDbConnection(connString))
  37. {
  38. PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
  39. intval=cmd.ExecuteNonQuery();
  40. //清除cmd的参数
  41. cmd.Parameters.Clear();
  42. if(conn.State==ConnectionState.Open)
  43. {
  44. conn.Close();
  45. }
  46. returnval;
  47. }
  48. }
  49. publicstaticintExecuteNonQuery(OleDbConnectionconn,CommandTypecmdType,stringcmdText,paramsOleDbParameter[]cmdParms)
  50. {
  51. OleDbCommandcmd=newOleDbCommand();
  52. PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
  53. intval=cmd.ExecuteNonQuery();
  54. cmd.Parameters.Clear();
  55. if(conn.State==ConnectionState.Open)
  56. {
  57. conn.Close();
  58. }
  59. returnval;
  60. }
  61. publicstaticintExecuteNonQuery(OleDbTransactiontrans,CommandTypecmdType,stringcmdText,paramsOleDbParameter[]cmdParms)
  62. {
  63. OleDbCommandcmd=newOleDbCommand();
  64. PrepareCommand(cmd,trans.Connection,trans,cmdType,cmdText,cmdParms);
  65. intval=cmd.ExecuteNonQuery();
  66. cmd.Parameters.Clear();
  67. if(cmd.Connection.State==ConnectionState.Open)
  68. {
  69. cmd.Connection.Close();
  70. }
  71. returnval;
  72. }
  73. #endregion
  74. #region=ExecuteReader=
  75. publicstaticOleDbDataReaderExecuteReader(stringconnectionString,CommandTypecommandType,stringcommandText)
  76. {
  77. //passthroughthecallprovidingnullforthesetofOleDbParameters
  78. returnExecuteReader(connectionString,commandType,commandText,(OleDbParameter[])null);
  79. }
  80. publicstaticOleDbDataReaderExecuteReader(stringconnString,CommandTypecmdType,stringcmdText,paramsOleDbParameter[]cmdParms)
  81. {
  82. OleDbCommandcmd=newOleDbCommand();
  83. OleDbConnectionconn=newOleDbConnection(connString);
  84. try
  85. {
  86. PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
  87. OleDbDataReaderrdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
  88. cmd.Parameters.Clear();
  89. if(conn.State==ConnectionState.Open)
  90. {
  91. conn.Close();
  92. }
  93. returnrdr;
  94. }
  95. catch
  96. {
  97. conn.Close();
  98. throw;
  99. }
  100. }
  101. #endregion
  102. #region=ExecuteDataset=
  103. publicstaticDataSetExecuteDataset(stringconnectionString,CommandTypecommandType,stringcommandText)
  104. {
  105. returnExecuteDataset(connectionString,commandType,commandText,(OleDbParameter[])null);
  106. }
  107. publicstaticDataSetExecuteDataset(stringconnectionString,CommandTypecommandType,stringcommandText,paramsOleDbParameter[]commandParameters)
  108. {
  109. using(OleDbConnectioncn=newOleDbConnection(connectionString))
  110. {
  111. cn.Open();
  112. //调用重载方法
  113. returnExecuteDataset(cn,commandType,commandText,commandParameters);
  114. }
  115. }
  116. publicstaticDataSetExecuteDataset(OleDbConnectionconnection,CommandTypecommandType,stringcommandText)
  117. {
  118. returnExecuteDataset(connection,commandType,commandText,(OleDbParameter[])null);
  119. }
  120. publicstaticDataSetExecuteDataset(OleDbConnectionconnection,CommandTypecommandType,stringcommandText,paramsOleDbParameter[]commandParameters)
  121. {
  122. //创建一个OleDbCommand对象,并对其进行初始化
  123. OleDbCommandcmd=newOleDbCommand();
  124. PrepareCommand(cmd,connection,(OleDbTransaction)null,commandType,commandText,commandParameters);
  125. //创建OleDbDataAdapter对象以及DataSet
  126. OleDbDataAdapterda=newOleDbDataAdapter(cmd);
  127. DataSetds=newDataSet();
  128. //填充ds
  129. da.Fill(ds);
  130. //清除cmd的参数集合
  131. cmd.Parameters.Clear();
  132. if(cmd.Connection.State==ConnectionState.Open)
  133. {
  134. cmd.Connection.Close();
  135. }
  136. //返回ds
  137. returnds;
  138. }
  139. #endregion
  140. #region=ExecuteDataTable=
  141. publicstaticDataTableExecuteDataTable(stringconnectionString,CommandTypecommandType,stringcommandText)
  142. {
  143. returnExecuteDataTable(connectionString,commandType,commandText,(OleDbParameter[])null);
  144. }
  145. publicstaticDataTableExecuteDataTable(stringconnectionString,CommandTypecommandType,stringcommandText,paramsOleDbParameter[]commandParameters)
  146. {
  147. using(OleDbConnectioncn=newOleDbConnection(connectionString))
  148. {
  149. cn.Open();
  150. //调用重载方法
  151. returnExecuteDataTable(cn,commandType,commandText,commandParameters);
  152. }
  153. }
  154. publicstaticDataTableExecuteDataTable(OleDbConnectionconnection,CommandTypecommandType,stringcommandText)
  155. {
  156. returnExecuteDataTable(connection,commandType,commandText,(OleDbParameter[])null);
  157. }
  158. publicstaticDataTableExecuteDataTable(OleDbConnectionconnection,CommandTypecommandType,stringcommandText,paramsOleDbParameter[]commandParameters)
  159. {
  160. //创建一个OleDbCommand对象,并对其进行初始化
  161. OleDbCommandcmd=newOleDbCommand();
  162. PrepareCommand(cmd,connection,(OleDbTransaction)null,commandType,commandText,commandParameters);
  163. //创建OleDbDataAdapter对象以及DataSet
  164. OleDbDataAdapterda=newOleDbDataAdapter(cmd);
  165. DataSetds=newDataSet();
  166. //填充ds
  167. da.Fill(ds);
  168. //清除cmd的参数集合
  169. cmd.Parameters.Clear();
  170. if(cmd.Connection.State==ConnectionState.Open)
  171. {
  172. cmd.Connection.Close();
  173. }
  174. //返回ds
  175. returnds.Tables[0];
  176. }
  177. #endregion
  178. #region=ExecuteScalar=
  179. publicstaticobjectExecuteScalar(stringconnString,CommandTypecmdType,stringcmdText)
  180. {
  181. returnExecuteScalar(connString,cmdType,cmdText,null);
  182. }
  183. publicstaticobjectExecuteScalar(stringconnString,CommandTypecmdType,stringcmdText,paramsOleDbParameter[]cmdParms)
  184. {
  185. OleDbCommandcmd=newOleDbCommand();
  186. using(OleDbConnectionconn=newOleDbConnection(connString))
  187. {
  188. PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
  189. objectval=cmd.ExecuteScalar();
  190. cmd.Parameters.Clear();
  191. if(conn.State==ConnectionState.Open)
  192. {
  193. conn.Close();
  194. }
  195. returnval;
  196. }
  197. }
  198. publicstaticobjectExecuteScalar(OleDbConnectionconn,CommandTypecmdType,stringcmdText)
  199. {
  200. returnExecuteScalar(conn,cmdType,cmdText,null);
  201. }
  202. publicstaticobjectExecuteScalar(OleDbConnectionconn,CommandTypecmdType,stringcmdText,paramsOleDbParameter[]cmdParms)
  203. {
  204. OleDbCommandcmd=newOleDbCommand();
  205. PrepareCommand(cmd,conn,null,cmdType,cmdText,cmdParms);
  206. objectval=cmd.ExecuteScalar();
  207. cmd.Parameters.Clear();
  208. if(conn.State==ConnectionState.Open)
  209. {
  210. conn.Close();
  211. }
  212. returnval;
  213. }
  214. #endregion
  215. publicstaticvoidCacheParameters(stringcacheKey,paramsOleDbParameter[]cmdParms)
  216. {
  217. parmCache[cacheKey]=cmdParms;
  218. }
  219. publicstaticOleDbParameter[]GetCachedParameters(stringcacheKey)
  220. {
  221. OleDbParameter[]cachedParms=(OleDbParameter[])parmCache[cacheKey];
  222. if(cachedParms==null)
  223. returnnull;
  224. OleDbParameter[]clonedParms=newOleDbParameter[cachedParms.Length];
  225. for(inti=0,j=cachedParms.Length;i<j;i++)
  226. clonedParms[i]=(OleDbParameter)((ICloneable)cachedParms[i]).Clone();
  227. returnclonedParms;
  228. }
  229. publicstaticvoidPrepareCommand(OleDbCommandcmd,OleDbConnectionconn,OleDbTransactiontrans,CommandTypecmdType,stringcmdText,OleDbParameter[]cmdParms)
  230. {
  231. //判断连接的状态。如果是关闭状态,则打开
  232. if(conn.State!=ConnectionState.Open)
  233. conn.Open();
  234. //cmd属性赋值
  235. cmd.Connection=conn;
  236. cmd.CommandText=cmdText;
  237. //是否需要用到事务处理
  238. if(trans!=null)
  239. cmd.Transaction=trans;
  240. cmd.CommandType=cmdType;
  241. //添加cmd需要的存储过程参数
  242. if(cmdParms!=null)
  243. {
  244. foreach(OleDbParameterparmincmdParms)
  245. cmd.Parameters.Add(parm);
  246. }
  247. }
  248. }
  249. }
使用方法跟SqlHelper类相似。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值