视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
C#操作Access实例解析
2020-11-09 07:39:36 责编:小采
文档


C#操作Access实例是怎么实现的呢?让我们来看看具体的代码: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.We

C#操作Access实例是怎么实现的呢?让我们来看看具体的代码:

  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.OleDb;
  11. ///
  12. /// DataAccess 的摘要说明 C#操作Access实例解析
  13. ///
  14. public class DataAccess
  15. {
  16. protected static OleDbConnection conn = new OleDbConnection();
  17. protected static OleDbCommand comm = new OleDbCommand();
  18. public DataAccess()
  19. {
  20. //init C#操作Access实例解析
  21. }
  22. private static void openConnection()
  23. {
  24. if (conn.State == ConnectionState.Closed)
  25. {
  26. conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;
  27. Data Source="+ConfigurationManager.AppSettings["myconn"];
  28. //web.config文件里设定。
  29. comm.Connection = conn;
  30. try
  31. {
  32. conn.Open();
  33. }
  34. catch (Exception e)
  35. { throw new Exception(e.Message); }
  36. }
  37. }//打开数据库 C#操作Access实例解析
  38. private static void closeConnection()
  39. {
  40. if (conn.State == ConnectionState.Open)
  41. {
  42. conn.Close();
  43. conn.Dispose();
  44. comm.Dispose();
  45. }
  46. }//关闭数据库 C#操作Access实例解析
  47. public static void excuteSql(string sqlstr)
  48. {
  49. try
  50. {
  51. openConnection();
  52. comm.CommandType = CommandType.Text;
  53. comm.CommandText = sqlstr;
  54. comm.ExecuteNonQuery();
  55. }
  56. catch (Exception e)
  57. {
  58. throw new Exception(e.Message);
  59. }
  60. finally
  61. { closeConnection(); }
  62. }//执行sql语句 C#操作Access实例解析
  63. public static OleDbDataReader dataReader(string sqlstr)
  64. {
  65. OleDbDataReader dr = null;
  66. try
  67. {
  68. openConnection();
  69. comm.CommandText = sqlstr;
  70. comm.CommandType = CommandType.Text;
  71. dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
  72. }
  73. catch
  74. {
  75. try
  76. {
  77. dr.Close();
  78. closeConnection();
  79. }
  80. catch { }
  81. }
  82. return dr;
  83. }
  84. //返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
  85. public static void dataReader(string sqlstr,
  86. ref OleDbDataReader dr)
  87. {
  88. try
  89. {
  90. openConnection();
  91. comm.CommandText = sqlstr;
  92. comm.CommandType = CommandType.Text;
  93. dr=comm.ExecuteReader(CommandBehavior.CloseConnection);
  94. }
  95. catch
  96. {
  97. try
  98. {
  99. if (dr != null && !dr.IsClosed)
  100. dr.Close();
  101. } //C#操作Access实例解析
  102. catch
  103. {
  104. }
  105. finally
  106. {
  107. closeConnection();
  108. }
  109. }
  110. }
  111. //返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
  112. public static DataSet dataSet(string sqlstr)
  113. {
  114. DataSet ds = new DataSet();
  115. OleDbDataAdapter da = new OleDbDataAdapter();
  116. try
  117. {
  118. openConnection();
  119. comm.CommandType = CommandType.Text;
  120. comm.CommandText = sqlstr;
  121. da.SelectCommand = comm;
  122. da.Fill(ds);
  123. }
  124. catch (Exception e)
  125. {
  126. throw new Exception(e.Message);
  127. }
  128. finally
  129. {
  130. closeConnection();
  131. }
  132. return ds;
  133. }//返回指定sql语句的dataset C#操作Access实例解析
  134. public static void dataSet(
  135. string sqlstr, ref DataSet ds)
  136. {
  137. OleDbDataAdapter da = new OleDbDataAdapter();
  138. try
  139. {
  140. openConnection();
  141. comm.CommandType = CommandType.Text;
  142. comm.CommandText = sqlstr;
  143. da.SelectCommand = comm;
  144. da.Fill(ds);
  145. }
  146. catch (Exception e)
  147. {
  148. throw new Exception(e.Message);
  149. }
  150. finally
  151. {
  152. closeConnection();
  153. }
  154. }//返回指定sql语句的dataset C#操作Access实例解析
  155. public static DataTable dataTable(string sqlstr)
  156. {
  157. DataTable dt = new DataTable();
  158. OleDbDataAdapter da = new OleDbDataAdapter();
  159. try
  160. {
  161. openConnection();
  162. comm.CommandType = CommandType.Text;
  163. comm.CommandText = sqlstr;
  164. da.SelectCommand = comm;
  165. da.Fill(dt);
  166. }
  167. catch (Exception e)
  168. {
  169. throw new Exception(e.Message);
  170. }
  171. finally
  172. {
  173. closeConnection();
  174. }
  175. return dt;
  176. }//返回指定sql语句的datatable
  177. public static void dataTable(
  178. string sqlstr, ref DataTable dt)
  179. {
  180. OleDbDataAdapter da = new OleDbDataAdapter();
  181. try
  182. {
  183. openConnection();
  184. comm.CommandType = CommandType.Text;
  185. comm.CommandText = sqlstr;
  186. da.SelectCommand = comm;
  187. da.Fill(dt);
  188. }
  189. catch (Exception e)
  190. {
  191. throw new Exception(e.Message);
  192. }
  193. finally
  194. {
  195. closeConnection();
  196. }
  197. }//返回指定sql语句的datatable C#操作Access实例解析
  198. public static DataView dataView(string sqlstr)
  199. {
  200. OleDbDataAdapter da = new OleDbDataAdapter();
  201. DataView dv = new DataView();
  202. DataSet ds = new DataSet();
  203. try
  204. {
  205. openConnection();
  206. comm.CommandType = CommandType.Text;
  207. comm.CommandText = sqlstr;
  208. da.SelectCommand = comm;
  209. da.Fill(ds);
  210. dv = ds.Tables[0].DefaultView;
  211. }
  212. catch (Exception e)
  213. {
  214. throw new Exception(e.Message);
  215. }
  216. finally
  217. {
  218. closeConnection();
  219. }
  220. return dv;
  221. }
  222. //返回指定sql语句的dataview C#操作Access实例解析
  223. }

C#操作Access实例解析的基本内容就向你介绍到这里,希望对你了解和学习C#操作Access有所帮助。

下载本文
显示全文
专题