c# access oledb helper class
连接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的更多相关文章
- ODBC、OLEDB和ADO之间的关系 ,以及性能比较
学习了.net视频之后,对里面涉及到的数据库连接部分中的一些概念表示很无语.网上很多相关资料,但除了网站不一样外,基本上内容都神一样的一致. 现在,我就通过结合看到的一些资料再加上自己的理解试图去解释 ...
- Groovy 处理 XML
1. Parsing XML 1.1. XmlParser and XmlSlurper The most commonly used approach for parsing XML with Gr ...
- PDF.NET 开发框架之 SOD框架 Ver 5.2 正式版开源源码发布
PDF.NET 开发框架之 SOD框架 Ver 5.2.1.0307 正式版发布,包含以下部分: SOD_Pwmis.Core --包括下列数据提供程序 SqlServer SqlServerCe A ...
- Spring MVC 使用拦截器优雅地实现权限验证功能
在上一篇 SpringAOP 实现功能权限校验功能 中虽然用AOP通过抛异常,请求转发等勉强地实现了权限验证功能,但感觉不是那么完美,应该用拦截器来实现才是最佳的,因为拦截器就是用来拦截请求的,在请求 ...
- 客官,您的 Flask 全家桶请收好
http://www.factj.com/archives/543.html Flask-AppBuilder - Simple and rapid Application buil ...
- DevExpress XPO 开发指南 简要
最近在看devexpress 安装程序中的代码Demos .. C:\Users\Public\Documents\DevExpress Demos 16.1\Components\WinFor ...
- 一个很好用的SqlHelper类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- XPO开发指南简要
一.XPO简介: XPO即eXpress Persistent Objects for .NET,现在这里介绍的版本是1.5. XPO在应用程序代码和数据库之间扮演了一个中间层的角色,简单而言,就是将 ...
- System.Data.OleDb操作access数据库类,【bubuko.com】
access数据库在应用了System.Data.OleDb后操作会很方便,这是一个常用的数据库操作类,其中两个方法,一个是返回datatable的,一个是执行sql语句返回影响记录的(一般是inse ...
随机推荐
- Rust学习笔记1
这是一份不错的rust教程,目前包括4个block和4个project.全部完成后可以用rust实现一个简单的key-value存储引擎. 注意:Windows下rust貌似会遇到一些bug,强烈建议 ...
- django报错处理:对应ip无法登陆与使用bootstrap移动端响应工具,head响应实例
1.报错 Invalid HTTP_HOST header: '192.168.1.100:8000'. You may need to add '192.168.1.100' to ALLOWED_ ...
- Response笔记
# 今日内容 1. HTTP协议:响应消息 2. Response对象 3. ServletContext对象 ## HTTP协议: 1. 请求消息:客户端发 ...
- jQuery中$()可以有两个参数
jQuery(expression, [context]) 返回值:jQuery 概述 这个函数接收一个包含 CSS 选择器的字符串,然后用这个字符串去匹配一组元素. jQuery 的核心功能都是通过 ...
- vue filters过滤
<template> <div class="filters"> <h1 v-text="filtersTitle">< ...
- Python爬虫之urllib.parse详解
Python爬虫之urllib.parse 转载地址 Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url 解析url( urlparse() ) ur ...
- Linux vim程序编辑器
Tips: 在 vi 里面, [tab] 这个按钮所得到的结果与空格符所得到的结果是不一样的,特别强调一下! 一般模式 移动光标 30↓ 向下移动30行 40→ 向右移动40个字符 gg 移动到档案第 ...
- 下载放在resource下面的excel文件
1.将excel文件放项目resources目录下 2.打包的时候排除指定后缀文件,否则打包时会出现文件损坏的情况 <configuration> <encoding>UTF- ...
- 35.ES6语法介绍——2019年12月24日
2019年12月24日16:22:24 2019年10月09日12:04:44 1. ES6介绍 1.1 新的 Javascript 语法标准 --2015年6月正式发布 --使用babel语法转换器 ...
- 【CF487E】Tourists
洛谷链接 题意 给定一张无向图,点有点权,多次询问两点之间路径上点权最小的点的可能的最小值,支持修改点权. Sol 一棵树就很简单 , 于是我们考虑点双(环)上的情况 , 直观想法就是缩完点双后一个点 ...