连接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;
}
}
}

  

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. tensorflow学习笔记二----------变量

    tensorflow里面的变量表示,需要使用特定的语法进行.如果想构造一个行(列)向量,需要调用Variable函数进行.对两个变量进行操作,也要调用相应的函数. import tensorflow ...

  2. [LeetCode] 154. 寻找旋转排序数组中的最小值 II

    题目链接 : https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array-ii/ 题目描述: 假设按照升序排序的数组在预 ...

  3. 通过编写串口助手工具学习MFC过程——(一)工程新建

    通过编写串口助手工具学习MFC过程 因为以前也做过几次MFC的编程,每次都是项目完成时,MFC基本操作清楚了,但是过好长时间不再接触MFC的项目,再次做MFC的项目时,又要从头开始熟悉.这次通过做一个 ...

  4. 剑指offer-用两个栈来实现一个队列-队列与栈-python

    用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路:使用两个栈,stackA 用来接收node stackB 用来接收 stackA 的出栈 # -*- cod ...

  5. getchar、putchar、puts、gets

    getchar(字符)  输入获取一个字符 putchar(字符)  输出控制台一个字符 scanf()格式化输入 printf() 格式化输出 gets(arr) 输入一个字符串给已经声明的数组ar ...

  6. mysql,oracle,sql server数据库默认的端口号,端口号可以为负数吗?以及常用协议所对应的缺省端口号

    mysql,oracle,sql server数据库默认的端口号? mysql:3306 Oracle:1521 sql server:1433 端口号可以为负吗? 不可以,端口号都有范围的,0~65 ...

  7. Spark-Streaming获取kafka数据的两种方式:Receiver与Direct的方式

    简单理解为:Receiver方式是通过zookeeper来连接kafka队列,Direct方式是直接连接到kafka的节点上获取数据 Receiver 使用Kafka的高层次Consumer API来 ...

  8. Spring基础11——Bean的作用域

    1.Bean的作用域种类 Spring中的bean的作用域分为四种:singleton.prototype.session.request,后两种很少使用,下面我们主要来学习前两种 2.singlet ...

  9. STM32程序编写或调试犯过的错误

    1.宏定义后加了分号: eg: define NOKEY_PRES 0;      (❌) define NOKEY_PRES 0      (✔) 2.

  10. Vim 系列笔记一

    Vim 系列笔记一 Vim 简介 什么是VIM ? Vim 是从 Vi 发展出来的一个编辑器,是 Vi 的升级版.而 vi 则是 Unix .类Unix(Linux)系统中自带的编辑器. Vim/Vi ...