AccessHelper

原文:AccessHelper

代码:

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[0].DefaultView;
        }
        catch (Exception e)
        {
            throw new Exception(e.Message);
        }
        finally
        {
            closeConnection();
        }
        return dv;
    }
    #endregion

}

时间: 2024-10-14 11:59:15

AccessHelper的相关文章

C# AccessHelper 完整示例代码

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data; public class AccessHelper:IDisposable { private OleDbConnection m_conn = new OleDbConnection(); public static string DAT

一个可用的AccessHelper类

前些日子做项目用了下Access数据库,看了很多AccessHelper,感觉这个虽然不是最好的,却是可用的. 故此分享一下,以后看到更好的还会继续分享. using System; using System.Data; using System.Configuration; using System.Data.OleDb; using System.Collections; namespace DAL { /// <summary> /// AcceHelper 的摘要说明 /// <

C#的Access数据库操作 AccessHelper类

由于学校的安全级别提高了,我们以前做的网站使用拼接sql方法查询数据会被拦截,所以说网站就必须重构,在重构之余,复习了下AccessHelper类,随便复习了下C#数据库操作.下面我们就开始吧. 先说个工具类,就是池类Pool,很简单的一个类,直接上代码 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Data; using System.Data.

C# AccessHelper

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.OleDb; using System.Data; public class AccessHelper:IDisposable { private OleDbConnection m_conn = new OleDbConnection(); public static string DAT

AccessHelper类

1 using System; 2 using System.Data; 3 using System.Configuration; 4 using System.Data.OleDb; 5 using System.Collections; 6 using System.Windows.Forms; 7 using System.Security.Cryptography; 8 using System.Text; 9 /// <summary> 10 /// AcceHelper 的摘要说

Accesshelper.cs

using System; using System.Data; using System.Data.OleDb; using System.Collections; using System.IO; using System.Globalization; using System.Configuration; namespace ArticleManage { public class DataAccess { 定义#region 定义 protected OleDbCommand Comm;

Access大数据高效分页语句

oracle的分页查询可以利用rowid伪列. db2的分页查询可以利用row_number() over()聚合函数. mysql有limit. access仿佛先天缺陷,仅提供了top n.那如何利用top来实现分页查询呢? 假设在access中有表t1 createtable t1( tc1 varchar(50)notnullprimarykey, tc2 varchar(30), tc3 varchar(30) ) 随机插入20条数据.如果以每页5条来显示数据,如果要显示11至15条如

C#---数据库访问通用类、Access数据库操作类、mysql类 .[转]

原文链接 //C# 数据库访问通用类 (ADO.NET)using System;using System.Collections.Generic;using System.Text;using System.Data;using System.Data.SqlClient;using System.Configuration; namespace XXX{    /// <summary>    /// 针对SQL Server数据库操作的通用类           /// </sum

SQLHelper帮助类

通过分装的方法: public class SQLHelper { private static readonly string ConnectionString = ConfigurationManager.AppSettings["conn"].ToString(); //SqlParameter[] 方便传递数组 /// <summary> /// 主要用于封装Command对象的ExecuteNonQuery方法,用于数据的增删改 /// </summary&