代码:

using System;
using System.Data;
using System.Configuration;
using System.Data.OleDb;
using ahwildlife.Utils; /// <summary>
/// AccessHelper 的摘要说明
/// </summary>
public class AccessHelper
{
#region 变量
protected static OleDbConnection conn = new OleDbConnection();
protected static OleDbCommand comm = new OleDbCommand();
protected static string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
#endregion #region 构造函数
/// <summary>
/// 构造函数
/// </summary>
public AccessHelper()
{ }
#endregion #region 打开数据库
/// <summary>
/// 打开数据库
/// </summary>
private static void openConnection()
{
if (conn.State == ConnectionState.Closed)
{
conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
comm.Connection = conn;
try
{
conn.Open();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
#endregion #region 关闭数据库
/// <summary>
/// 关闭数据库
/// </summary>
private static void closeConnection()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
conn.Dispose();
comm.Dispose();
}
}
#endregion #region 执行sql语句
/// <summary>
/// 执行sql语句
/// </summary>
public static void ExecuteSql(string sqlstr)
{
try
{
openConnection();
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
comm.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
}
#endregion #region 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
/// <summary>
/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
/// </summary>
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;
}
#endregion #region 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
/// <summary>
/// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭
/// </summary>
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();
}
}
}
#endregion #region 返回指定sql语句的DataSet
/// <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;
}
#endregion #region 返回指定sql语句的DataSet
/// <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();
}
}
#endregion #region 返回指定sql语句的DataTable
/// <summary>
/// 返回指定sql语句的DataTable
/// </summary>
/// <param name="sqlstr"></param>
/// <returns></returns>
public static DataTable DataTable(string sqlstr)
{
DataTable dt = Common.GetDataTableCache(sqlstr);//读缓存
if (dt != null)
{
return dt.Copy();
}
else
{
dt = new DataTable();
OleDbDataAdapter da = new OleDbDataAdapter();
try
{
using (OleDbConnection conn = new OleDbConnection())
{
conn.ConnectionString = connectionString;
conn.Open();
using (OleDbCommand comm = new OleDbCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = sqlstr;
da.SelectCommand = comm;
da.Fill(dt);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
Common.InsertDataTableCache(sqlstr, dt);//添加缓存
return dt.Copy();
}
}
#endregion #region 返回指定sql语句的DataTable
/// <summary>
/// 返回指定sql语句的DataTable
/// </summary>
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();
}
}
#endregion #region 返回指定sql语句的DataView
/// <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[].DefaultView;
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
closeConnection();
}
return dv;
}
#endregion }

AccessHelper的更多相关文章

  1. AccessHelper类

    using System; using System.Data; using System.Configuration; using System.Data.OleDb; using System.C ...

  2. 连接ACCESS的AccessHelper.cs类

    using System; using System.Data; using System.Configuration; using System.Data.OleDb; using System.C ...

  3. Access2007数据库下载地址与AccessHelper

    链接:https://pan.baidu.com/s/1pLzOlTv0nqSbhzujHZht1w 提取码:1m9l AccessHelper: using System; using System ...

  4. AccessHelper 需修改

    using System; using System.Collections.Generic; using System.Configuration; using System.Data; using ...

  5. Accesshelper.cs

    using System; using System.Data; using System.Data.OleDb; using System.Collections; using System.IO; ...

  6. 关于SqlHelper的详解

    SqlHelper是一个基于.NET Framework的数据库操作组件.组件中包含数据库操作方法.SqlHelper用于简化你重复的去写那些数据库连接(SqlConnection),SqlComma ...

  7. NPOI操作excel

    1.基本导出方法 private void ExportToExcel() { SaveFileDialog sdfExport = new SaveFileDialog(); sdfExport.F ...

  8. C# 对Access数据库操作的通用类

    (转载自博主Jerry很简单) //Access数据库-C# 操作类 代码using System;using System.Collections.Generic;using System.Linq ...

  9. 数据采集:完美下载淘宝Ip数据库 简单的程序节省60元人民币而不必购买数据库

    曾经做网站类型的程序时,经常需要收集客户端的访问数据,然后加以分析.这需要一个Ip数据库,数据表中显示Ip所在的省份市区等信息.网络上有流传的Ip纯真数据库,一些公开的Web服务也可以查询Ip地址信息 ...

随机推荐

  1. ubuntu 休眠之后网络间接失败 can not connect to network after suspend (wake up)

    ubuntu for laptop系统在系统休眠后wakeup 之后,网络连接失败, 有线网络无法连接, 无线wifi无法连接, 只能重启后才能恢复, 此时可以采用以下方法处理: 1. 在/etc/p ...

  2. javascript - 二叉树

    都是些简单的东西,所以直接上代码了. /** * Created by huangjacky on 14-10-3. */ function Node(element, left, right) { ...

  3. 使用 ContentProviderOperation 来提升性能

    ContentProviders  是android 系统核心组件之一,ContentProviders 封装了数据的访问接口,其底层数据一般都是保存在数据库中或者保存在云端. 有时候你需要更新多行数 ...

  4. html5media.js 让浏览器兼容<Video><Audio> 标签

    介绍:https://html5media.info/ 项目:https://github.com/etianen/html5media Wiki:https://github.com/etianen ...

  5. 【转帖】ECLIPSE-JEE-LUNA-SR2官方汉化教程

    ECLIPSE-JEE-LUNA-SR2官方汉化教程 工具/原料 Eclipse-jee-luna-SR2 步骤/方法 1.在浏览器输入网址http://www.eclipse.org/babel/d ...

  6. IT部门能力评估...

    IT运行成本和变化成本越来越高,IT部门是否上了一些对企业无价值的系统,是否充分利用了已有系统的价值? 随 着IT应用不断深入,庞大的企业IT系统日积月累,各种隐患渐渐显露.IT系统变得越来越复杂,运 ...

  7. C++ STL轻松导学

    作为C++标准不可缺少的一部分,STL应该是渗透在C++程序的角角落落里的.STL不是实验室里的宠儿,也不是程序员桌上的摆设,她的激动人心并非昙花一现.本教程旨在传播和普及STL的基础知识,若能借此机 ...

  8. RedRabbit——基于BrokerPattern服务器框架

    RedRabbit 经典网游服务器架构 该图省略了专门用途的dbserver.guildserver等用于专门功能的server,该架构的优点有: l LoginGate相当于DNS,可以动态的保证G ...

  9. 简化 Hadoop 2.4.1 Eclpse 插件编译【原创】

    昨天折腾hadoop2X的eclipse插件,从https://github.com/winghc/hadoop2x-eclipse-plugin把源码搞下来后,很快搞定出来一个,但是...New H ...

  10. C++ Low level performance optimize 2

    C++ Low level performance optimize 2 上一篇 文章讨论了一些底层代码的优化技巧,本文继续讨论一些相关的内容. 首先,上一篇文章讨论cache missing的重要性 ...