代码:

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. Android防止按钮连续点击

    为了防止用户或者测试MM疯狂的点击某个button,写个方法防止按钮连续点击. public class Utils { private static long lastClickTime; publ ...

  2. Difference between Satisfiable, Valid, Unsatisfiable & Invalid

    A formula is satisfiable if it is possible to find an interpretation (model) that makes the formula  ...

  3. iOS杂谈-图片拉伸的实现

    如上图是一个按钮的背景图,在Android上,很多图片资源都是类似这样子的,但是由于按钮的高度及宽度与图片的世纪尺寸不同,所以需要采用9patch来实现拉伸处理, 可参考:http://www.cnb ...

  4. SQL的主键和外键约束

    SQL的主键和外键的作用: 外键取值规则:空值或参照的主键值. (1)插入非空值时,如果主键表中没有这个值,则不能插入. (2)更新时,不能改为主键表中没有的值. (3)删除主键表记录时,你可以在建外 ...

  5. Hive Streaming 追加 ORC 文件

    1.概述 在存储业务数据的时候,随着业务的增长,Hive 表存储在 HDFS 的上的数据会随时间的增加而增加,而以 Text 文本格式存储在 HDFS 上,所消耗的容量资源巨大.那么,我们需要有一种方 ...

  6. 算法 - 求两个自然数的最小公倍数(C++)

    //************************************************************************************************** ...

  7. 解决oracle 端口 1521 本机127可通 其他ip不通

    http://wenku.baidu.com/link?url=8tRGGObqgLd6-yqprioIZSyluu9K0BgA29Lhx7F57pVDIHbMHVDNTa_SlEmVugGT4QJO ...

  8. CSS 会被继承的属性

    文本 color(颜色,a元素除外) direction(方向) font(字体) font-family(字体系列) font-size(字体大小) font-style(用于设置斜体) font- ...

  9. svn diff excel

    https://github.com/solq360/compareExcel jdk 1.7 可自己编译1.6 每个sheet第一行不允许为空 SVN对比excel差异 适合策划.财务职业使用 sv ...

  10. Malek Dance Club(递推)

    Malek Dance Club time limit per test 1 second memory limit per test 256 megabytes input standard inp ...