连接Access数据库

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System;
  6. using System.Data;
  7. using System.Configuration;
  8. using System.Web;
  9. using System.Data.OleDb;
  10.  
  11. namespace TestAccessDB
  12. {
  13.  
  14. public class DBHelper
  15. {
  16. protected static OleDbConnection conn = new OleDbConnection();
  17. protected static OleDbCommand comm = new OleDbCommand();
  18.  
  19. public DBHelper()
  20. {
  21.  
  22. }
  23. static string dbFile = AppDomain.CurrentDomain.BaseDirectory + "\\goods.mdb";
  24.  
  25. private static void openConnection()
  26. {
  27. if (conn.State == ConnectionState.Closed)
  28. {
  29. // conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + dbFile + ";Jet OLEDB:Database PassWord=sa";
  30. // conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ dbFile +";Persist Security Info=False;Jet OLEDB:Database Password=sa;";
  31. conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile + ";Persist Security Info=False;";
  32. comm.Connection = conn;
  33. try
  34. {
  35. conn.Open();
  36. }
  37. catch (Exception e)
  38. { throw new Exception(e.Message); }
  39.  
  40. }
  41.  
  42. }
  43. private static void closeConnection()
  44. {
  45. if (conn.State == ConnectionState.Open)
  46. {
  47. conn.Close();
  48. conn.Dispose();
  49. comm.Dispose();
  50. }
  51. }
  52. /// <summary>
  53. /// 执行sql语句
  54. /// </summary>
  55. /// <param name="sqlstr"></param>
  56. public static int ExecuteNonQuery(string sqlstr)
  57. {
  58. int i = 0;
  59. try
  60. {
  61. openConnection();
  62. comm.CommandType = CommandType.Text;
  63. comm.CommandText = sqlstr;
  64. i= comm.ExecuteNonQuery();
  65. }
  66. catch (Exception e)
  67. {
  68. throw new Exception(e.Message);
  69. }
  70. finally
  71. {
  72.  
  73. closeConnection();
  74.  
  75. }
  76. return i;
  77. }
  78.  
  79. /// <summary>
  80. /// 执行sql语句
  81. /// </summary>
  82. /// <param name="sqlstr"></param>
  83. public static object executeScalarSql(string sqlstr)
  84. {
  85. object o;
  86. try
  87. {
  88. openConnection();
  89. comm.CommandType = CommandType.Text;
  90. comm.CommandText = sqlstr;
  91. o= comm.ExecuteScalar();
  92. }
  93. catch (Exception e)
  94. {
  95. throw new Exception(e.Message);
  96. }
  97. finally
  98. {
  99.  
  100. closeConnection();
  101.  
  102. }
  103.  
  104. return o;
  105. }
  106.  
  107. public static int batchInsert(int count)
  108. {
  109.  
  110. int cnt = 0;
  111.  
  112. try
  113. {
  114.  
  115. for (int i = 1; i <= count; i++)
  116. {
  117. string sqlstr = "insert into article(sourceID,[user],updateDate) values ('454545434','qwertteeeew_gfhfghgfh',#" + DateTime.Now + "#)";
  118.  
  119. openConnection();
  120. comm.CommandType = CommandType.Text;
  121. comm.CommandText = sqlstr;
  122. cnt += comm.ExecuteNonQuery();
  123.  
  124. }
  125.  
  126. }
  127. catch (Exception e)
  128. {
  129. throw new Exception(e.Message);
  130. }
  131. finally
  132. {
  133.  
  134. closeConnection();
  135.  
  136. }
  137.  
  138. return cnt;
  139.  
  140. }
  141.  
  142. /// <summary>
  143. /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
  144. /// </summary>
  145. /// <param name="sqlstr"></param>
  146. /// <returns></returns>
  147. public static OleDbDataReader dataReader(string sqlstr)
  148. {
  149. OleDbDataReader dr = null;
  150. try
  151. {
  152. openConnection();
  153. comm.CommandText = sqlstr;
  154. comm.CommandType = CommandType.Text;
  155.  
  156. dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
  157. }
  158. catch
  159. {
  160. try
  161. {
  162. dr.Close();
  163. closeConnection();
  164. }
  165. catch { }
  166. }
  167. return dr;
  168. }
  169. /// <summary>
  170. /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
  171. /// </summary>
  172. /// <param name="sqlstr"></param>
  173. /// <param name="dr"></param>
  174. public static void dataReader(string sqlstr, ref OleDbDataReader dr)
  175. {
  176. try
  177. {
  178. openConnection();
  179. comm.CommandText = sqlstr;
  180. comm.CommandType = CommandType.Text;
  181. dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
  182. }
  183. catch
  184. {
  185. try
  186. {
  187. if (dr != null && !dr.IsClosed)
  188. dr.Close();
  189. }
  190. catch
  191. {
  192. }
  193. finally
  194. {
  195. closeConnection();
  196. }
  197. }
  198. }
  199. /// <summary>
  200. /// 返回指定sql语句的dataset
  201. /// </summary>
  202. /// <param name="sqlstr"></param>
  203. /// <returns></returns>
  204. public static DataSet dataSet(string sqlstr)
  205. {
  206. DataSet ds = new DataSet();
  207. OleDbDataAdapter da = new OleDbDataAdapter();
  208. try
  209. {
  210. openConnection();
  211. comm.CommandType = CommandType.Text;
  212. comm.CommandText = sqlstr;
  213. da.SelectCommand = comm;
  214. da.Fill(ds);
  215.  
  216. }
  217. catch (Exception e)
  218. {
  219. throw new Exception(e.Message);
  220. }
  221. finally
  222. {
  223. closeConnection();
  224. }
  225. return ds;
  226. }
  227. /// <summary>
  228. /// 返回指定sql语句的dataset
  229. /// </summary>
  230. /// <param name="sqlstr"></param>
  231. /// <param name="ds"></param>
  232. public static void dataSet(string sqlstr, ref DataSet ds)
  233. {
  234. OleDbDataAdapter da = new OleDbDataAdapter();
  235. try
  236. {
  237. openConnection();
  238. comm.CommandType = CommandType.Text;
  239. comm.CommandText = sqlstr;
  240. da.SelectCommand = comm;
  241. da.Fill(ds);
  242. }
  243. catch (Exception e)
  244. {
  245. throw new Exception(e.Message);
  246. }
  247. finally
  248. {
  249. closeConnection();
  250. }
  251. }
  252. /// <summary>
  253. /// 返回指定sql语句的datatable
  254. /// </summary>
  255. /// <param name="sqlstr"></param>
  256. /// <returns></returns>
  257. public static DataTable dataTable(string sqlstr)
  258. {
  259. DataTable dt = new DataTable();
  260. OleDbDataAdapter da = new OleDbDataAdapter();
  261. try
  262. {
  263. openConnection();
  264. comm.CommandType = CommandType.Text;
  265. comm.CommandText = sqlstr;
  266. da.SelectCommand = comm;
  267. da.Fill(dt);
  268. }
  269. catch (Exception e)
  270. {
  271. throw new Exception(e.Message);
  272. }
  273. finally
  274. {
  275. closeConnection();
  276. }
  277. return dt;
  278. }
  279. /// <summary>
  280. /// 返回指定sql语句的datatable
  281. /// </summary>
  282. /// <param name="sqlstr"></param>
  283. /// <param name="dt"></param>
  284. public static void dataTable(string sqlstr, ref DataTable dt)
  285. {
  286. OleDbDataAdapter da = new OleDbDataAdapter();
  287. try
  288. {
  289. openConnection();
  290. comm.CommandType = CommandType.Text;
  291. comm.CommandText = sqlstr;
  292. da.SelectCommand = comm;
  293. da.Fill(dt);
  294. }
  295. catch (Exception e)
  296. {
  297. throw new Exception(e.Message);
  298. }
  299. finally
  300. {
  301. closeConnection();
  302. }
  303. }
  304. /// <summary>
  305. /// 返回指定sql语句的dataview
  306. /// </summary>
  307. /// <param name="sqlstr"></param>
  308. /// <returns></returns>
  309. public static DataView dataView(string sqlstr)
  310. {
  311. OleDbDataAdapter da = new OleDbDataAdapter();
  312. DataView dv = new DataView();
  313. DataSet ds = new DataSet();
  314. try
  315. {
  316. openConnection();
  317. comm.CommandType = CommandType.Text;
  318. comm.CommandText = sqlstr;
  319. da.SelectCommand = comm;
  320. da.Fill(ds);
  321. dv = ds.Tables[0].DefaultView;
  322. }
  323. catch (Exception e)
  324. {
  325. throw new Exception(e.Message);
  326. }
  327. finally
  328. {
  329. closeConnection();
  330. }
  331. return dv;
  332. }
  333. }
  334. }

  

c# access oledb helper class的更多相关文章

  1. ODBC、OLEDB和ADO之间的关系 ,以及性能比较

    学习了.net视频之后,对里面涉及到的数据库连接部分中的一些概念表示很无语.网上很多相关资料,但除了网站不一样外,基本上内容都神一样的一致. 现在,我就通过结合看到的一些资料再加上自己的理解试图去解释 ...

  2. Groovy 处理 XML

    1. Parsing XML 1.1. XmlParser and XmlSlurper The most commonly used approach for parsing XML with Gr ...

  3. PDF.NET 开发框架之 SOD框架 Ver 5.2 正式版开源源码发布

    PDF.NET 开发框架之 SOD框架 Ver 5.2.1.0307 正式版发布,包含以下部分: SOD_Pwmis.Core --包括下列数据提供程序 SqlServer SqlServerCe A ...

  4. Spring MVC 使用拦截器优雅地实现权限验证功能

    在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...

  5. 客官,您的 Flask 全家桶请收好

    http://www.factj.com/archives/543.html Flask-AppBuilder          - Simple and rapid Application buil ...

  6. DevExpress XPO 开发指南 简要

    最近在看devexpress   安装程序中的代码Demos ..  C:\Users\Public\Documents\DevExpress Demos 16.1\Components\WinFor ...

  7. 一个很好用的SqlHelper类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. XPO开发指南简要

    一.XPO简介: XPO即eXpress Persistent Objects for .NET,现在这里介绍的版本是1.5. XPO在应用程序代码和数据库之间扮演了一个中间层的角色,简单而言,就是将 ...

  9. System.Data.OleDb操作access数据库类,【bubuko.com】

    access数据库在应用了System.Data.OleDb后操作会很方便,这是一个常用的数据库操作类,其中两个方法,一个是返回datatable的,一个是执行sql语句返回影响记录的(一般是inse ...

随机推荐

  1. linux中文件权限格式与chmod命令以及用户和用户组的管理

    简单了解一下linux中的文件权限格式与chmod命令 chmod命令:改变文件或者目录的权限 格式:chmod [参数] [<权限范围><符号><权限代码>] - ...

  2. [七月挑选]Tomcat使用命令行启动之指定jdk版本

    title: Tomcat使用命令行启动之指定jdk版本 准备好环境,jdk和tomcat. 主要步骤 1.找到Tomcat/bin/catalina.bat文件. 2.在文件前端添加如下. set ...

  3. 安装sysbench,报错"Could not resolve 'ports.ubuntu.com'"

    在ubuntu系统中安装sysbench时报错“Could not resolve 'ports.ubuntu.com'”怎么办呢? 安装时报错: 亲测可用的方法: 修改 resolv.conf 文件 ...

  4. 022-OpenStack 中虚拟机hostname问题

    第一种: openstack中直接使用 hostnamectl 修改主机名,主机名在内核中的信息会被立即修改,但是当系统重启之后,主机名又重新变成原来的主机名称了.openstack主机名由cloud ...

  5. 02java基础——类和方法

    1.类的定义 /* 定义类: 使用类的形式,对现实中的事物进行描述 事物: 属性,方法 属性: 变量 方法: 这个事物具备的功能 格式: public class 类名{ 属性定义 修饰符 数据类型 ...

  6. C++中vecotr表示二维数组并自己实现一个Grid类

    1 C++中使用vector来表示二维数组 声明一个二维数组: vector<vector<int>> dp(row, vector<int>(col)); 将变量 ...

  7. vue-router解析,vue-router原理解析

    前言:新一季面试季,重新整理一些知识点: 本文详细说明自己对vue-router原理的理解: 参考: 源码:vuejs/vue-router v2.2.1 - github 文档:vue-router ...

  8. git分支管理与tag的学习笔记

    git分支管理学习笔记:创建dev分支:git branch dev查看分支:git branch切换分支:git checkout dev创建并切换分支:git checkout dev -b zh ...

  9. kettle中使用mysql的tinyint 类型到slqserver的tinyint类型

    各个数据库之间的类型 定义还是有差别的 一下是我在工作中遇到的一个很奇葩的问题  mysql 中的 tinyint 类型 插入到sqlserver 的tinyint 类型 插入到 sqlserver的 ...

  10. 并行操作多个序列map

    >>> def add1(a): return a + 1 >>> def add2(a,b): return a + b >>> def add ...