代码:

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. C语言代写

    MTRX1702 - C ProgrammingAssignment 2This assignment requires you to design and build a program that ...

  2. 4kbps~15kbps语音编码器基础框架ACELP

  3. 项目管理知识体系指南(PMBOOK指南)(第5版) 阅读摘要

    1.7.2 项目经理的人际技能 领导力: 团队建设: 激励: 沟通: 影响力: 决策能力: 政治和文化意识: 谈判: 建立信任: 冲突管理: 教练技术: 3.4 规划过程组 在制定项目管理计划和项目文 ...

  4. 让IE8支持HTML5及canvas功能!

    微软出的IE9支持HTML5,但因为不支持XP系统,暂时我还用不了. 即使能用,现阶段如果开发HTML5页面,并考虑到兼容性问题的话,恐怕也得让自己的界面支持IE6-8吧. 首先,需要让IE支持HTM ...

  5. 参照nopCommerce框架开发(NextCMS)

    很久没有更新博客了,现在已经不写.NET,转前端半年多了. 半年前在创业公司,做电子商务网站,用的是NopCommerce框架(3.2),这个框架还是相当不错的,经过一段时间的摸索,基本入门,于是就开 ...

  6. pidgin修改来消息字体大小

    vi ~/.gtkrc-2.0写入如下内容设置自己想要的字体大小 style "imhtml-fix"{    font_name = "Sans 14"} w ...

  7. win平台,apache通过web访问svn

    上个月用php写一个在线打包lua变成luac,碰到个权限问题.pysvn无法更新.网上搜了好半天都找不到解决方法.最好还是自己解决了. 解决方法也很简单,找到aphache服务,提权限就行了.lin ...

  8. POJ 2078 Matrix

    Matrix Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 3239   Accepted: 1680 Descriptio ...

  9. python 字符串查找

    python 字符串查找有4个方法,1 find,2 index方法,3 rfind方法,4 rindex方法. 1 find()方法: )##从下标1开始,查找在字符串里第一个出现的子串:返回结果3 ...

  10. ODBC连接问题

    http://zhidao.baidu.com/link?url=EPEMTuGC1q5wWavZigWseoHOwRLvpHyAVsdIgMLspErJOUZMEepIICUnT9IdkPQlYTm ...