连接Access数据库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Data.OleDb;namespace TestAccessDB
{public class DBHelper{protected static OleDbConnection conn = new OleDbConnection();protected static OleDbCommand comm = new OleDbCommand();public DBHelper(){}static string dbFile = AppDomain.CurrentDomain.BaseDirectory + "\\goods.mdb";private static void openConnection(){if (conn.State == ConnectionState.Closed){// conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + AppDomain.CurrentDomain.BaseDirectory + dbFile + ";Jet OLEDB:Database PassWord=sa";// conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+ dbFile +";Persist Security Info=False;Jet OLEDB:Database Password=sa;";conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbFile + ";Persist Security Info=False;";comm.Connection = conn;try{conn.Open();}catch (Exception e){ throw new Exception(e.Message); }}}private static void closeConnection(){if (conn.State == ConnectionState.Open){conn.Close();conn.Dispose();comm.Dispose();}}/// <summary>/// 执行sql语句/// </summary>/// <param name="sqlstr"></param>public static int ExecuteNonQuery(string sqlstr){int i = 0;try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;i= comm.ExecuteNonQuery();}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}return i;}/// <summary>/// 执行sql语句/// </summary>/// <param name="sqlstr"></param>public static object  executeScalarSql(string sqlstr){object o;try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;o= comm.ExecuteScalar();}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}return o;}public static int batchInsert(int count){int cnt = 0;try{for (int i = 1; i <= count; i++){string sqlstr = "insert into article(sourceID,[user],updateDate) values ('454545434','qwertteeeew_gfhfghgfh',#" + DateTime.Now + "#)";openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;cnt += comm.ExecuteNonQuery();}}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}return cnt;}/// <summary>/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。/// </summary>/// <param name="sqlstr"></param>/// <returns></returns>public static OleDbDataReader dataReader(string sqlstr){OleDbDataReader dr = null;try{openConnection();comm.CommandText = sqlstr;comm.CommandType = CommandType.Text;dr = comm.ExecuteReader(CommandBehavior.CloseConnection);}catch{try{dr.Close();closeConnection();}catch { }}return dr;}/// <summary>/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭/// </summary>/// <param name="sqlstr"></param>/// <param name="dr"></param>public static void dataReader(string sqlstr, ref OleDbDataReader dr){try{openConnection();comm.CommandText = sqlstr;comm.CommandType = CommandType.Text;dr = comm.ExecuteReader(CommandBehavior.CloseConnection);}catch{try{if (dr != null && !dr.IsClosed)dr.Close();}catch{}finally{closeConnection();}}}/// <summary>/// 返回指定sql语句的dataset/// </summary>/// <param name="sqlstr"></param>/// <returns></returns>public static DataSet dataSet(string sqlstr){DataSet ds = new DataSet();OleDbDataAdapter da = new OleDbDataAdapter();try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;da.SelectCommand = comm;da.Fill(ds);}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}return ds;}/// <summary>/// 返回指定sql语句的dataset/// </summary>/// <param name="sqlstr"></param>/// <param name="ds"></param>public static void dataSet(string sqlstr, ref DataSet ds){OleDbDataAdapter da = new OleDbDataAdapter();try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;da.SelectCommand = comm;da.Fill(ds);}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}}/// <summary>/// 返回指定sql语句的datatable/// </summary>/// <param name="sqlstr"></param>/// <returns></returns>public static DataTable dataTable(string sqlstr){DataTable dt = new DataTable();OleDbDataAdapter da = new OleDbDataAdapter();try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;da.SelectCommand = comm;da.Fill(dt);}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}return dt;}/// <summary>/// 返回指定sql语句的datatable/// </summary>/// <param name="sqlstr"></param>/// <param name="dt"></param>public static void dataTable(string sqlstr, ref DataTable dt){OleDbDataAdapter da = new OleDbDataAdapter();try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;da.SelectCommand = comm;da.Fill(dt);}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}}/// <summary>/// 返回指定sql语句的dataview/// </summary>/// <param name="sqlstr"></param>/// <returns></returns>public static DataView dataView(string sqlstr){OleDbDataAdapter da = new OleDbDataAdapter();DataView dv = new DataView();DataSet ds = new DataSet();try{openConnection();comm.CommandType = CommandType.Text;comm.CommandText = sqlstr;da.SelectCommand = comm;da.Fill(ds);dv = ds.Tables[0].DefaultView;}catch (Exception e){throw new Exception(e.Message);}finally{closeConnection();}return dv;}}
}

  

转载于:https://www.cnblogs.com/wgscd/p/11233137.html

c# access oledb helper class相关推荐

  1. ACCESS数据库连接字符串

    MS ACCESS ODBC开放式接口连接字符串 标准链接: "Driver= {MicrosoftAccessDriver(*.mdb)};DBQ=C:\App1\你的数据库名.mdb;U ...

  2. 数据库连接串MSSQL、Oracle、Access

    1.MSSQL连接字符串 (1)OLEDB Windows 身份验证 "Provider=SQLOLEDB;Data Source=PEK7-6TKX23X\SQLEXPRESS;Initi ...

  3. mysql odbc ado性能差异_ODBC、OLEDB和ADO之间的关系 ,以及性能比较

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

  4. 基于C#和ACCESS数据库实现的水电管理信息系统

    1 需求和规格说明 设计一个水电管理信息系统,能够对高校的水电费用进行管理,包括了登记费用,查询费用,以及住户信息管理等.在设计时要考虑到学生和教工在用水电时的不同,学生可以免费使用一定额度的水电,超 ...

  5. XPO 学习资料(转摘)

    Dev 出品的XPO是一个O/R Mapping框架,虽然是商业软件,非开源,但提供了源码.况且Dev的产品一向以精品为主,值得好好研究一下(我不是Dev的代理 ). 于是在学习过程中做个摘要. 一. ...

  6. DevExpress XPO 开发指南 简要

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

  7. KSP RSS bonk 希望各大大佬幫助一下

    錯誤日誌: Unity Player [version: Unity 5.4.0p4_b15b5ae035b7] mono.dll caused an Access Violation (0xc000 ...

  8. flask中的所有第三方模块大集合

    关于flask你想要的这里都有!!! Flask-AppBuilder - Simple and rapid Application builder, includes detailed securi ...

  9. ruby on rails_最终的中级Ruby on Rails教程:让我们创建一个完整的应用程序!

    ruby on rails 由Domantas G (By Domantas G) There are plenty tutorials online which show how to create ...

  10. C#访问Access和Win7 64位下可能遇到的 未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0” 提供程序 问题

    新建一个access数据库和表:录入几条数据:下面工具可在网上下: ac1.cs: using System; using System.Collections.Generic; using Syst ...

最新文章

  1. 加速产业生态算力升级,华为鲲鹏展翅福州
  2. 数据库系统概论:第三章 关系数据库标准语言SQL
  3. TCP、UDP数据包大小的限制
  4. [翻译:更新]Understanding Linux Network Internals - Table of Contents
  5. C语言学习及应用笔记之三:C语言const关键字及其使用
  6. python编码和解码_Python中的编码与解码(转)
  7. 涵盖各种编程语言的深度学习库整理大全!
  8. 可扩展的编程语言——Scala
  9. yum方式安装mysql多实例_centos7下使用yum安装mysql
  10. 新来的领导把我的职务免掉了,一年后,我要不要找领导聊聊?
  11. 【安卓笔记】—— 感知生命周期 Lifecycles
  12. arm poky linux,opencv移植在4412和imx6(yocto 3.14.28 arm-poky-linux-gnueabi )上
  13. android adb sdk下载地址,Android SDK 下载和安装
  14. 【jvm jdk】锁状态位之偏向锁
  15. catia中尺子没了怎么调出来_CATIA点线面工具栏怎么调出来
  16. 64位x86微服务器芯片,卖贝商城告诉你微服务器替代x86服务器的利与弊
  17. 【综合】系统架构设计师考试经历分享
  18. python推箱子小游戏_python写的推箱子小游戏
  19. 萌新卷妹带你逃出算法无名岛第五站
  20. 此beta版已额满_日志MIUI 11 第439周开发版内测日志补充

热门文章

  1. python中PIL.Image,OpenCV,Numpy图像格式相互转换
  2. HTML和XHTML解析(HTMLParser、BeautifulSoup)
  3. 3分钟了解计算机基础知识,你对电脑还一无所知?3分钟带你全面了解电脑基础知识...
  4. gitlab主备同步_gitlab实现主备切换集群
  5. php返回成功信息msg_PHP进化史 — 从v5.6到v8.0(可收藏)
  6. bootstrap4 图标和文字行内对齐
  7. Docker和Ubuntu主机互传复制文件
  8. php imagick下载,PHP安装imagemagick扩展imagick
  9. Kubernetes(K8s) 1.14.3常用操作命令
  10. 5)Thymeleaf 模板布局 th:fragment、th:replace、th:insert、th:remove