C#数据库操作类AccessHelper实例

 更新时间:2014年10月14日 15:23:11   投稿:shichen2014  
这篇文章主要介绍了C#数据库操作类AccessHelper实例,可实现针对access数据库的各种常见操作,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了C#数据库操作类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
}

希望本文所述对大家的C#程序设计有所帮助。

相关文章

  • C#实现将一个字符串进行翻转显示的6种方法

    C#实现将一个字符串进行翻转显示的6种方法

    下面小编就为大家分享一篇C#实现将一个字符串进行翻转显示的6种方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • C#读取CSV文件的方法总结

    C#读取CSV文件的方法总结

    CSV文件是一种简单的文本文件格式,用于存储表格数据,在C#中,有多种方法可以用于读取CSV文件,本文将介绍几种常见的读取CSV文件的方法,包括使用System.IO命名空间中的类、使用CsvHelper库以及使用LINQ,需要的朋友可以参考下
    2024-05-05
  • C#实现在网页中根据url截图并输出到网页的方法

    C#实现在网页中根据url截图并输出到网页的方法

    这篇文章主要介绍了C#实现在网页中根据url截图并输出到网页的方法,涉及C#网页浏览器及图片操作的相关技巧,需要的朋友可以参考下
    2016-01-01
  • WPF如何实现日期范围选择器

    WPF如何实现日期范围选择器

    这篇文章主要为大家详细介绍了WPF如何实现日期范围选择器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-11-11
  • C#规则引擎RulesEngine的具体使用

    C#规则引擎RulesEngine的具体使用

    这篇文章主要介绍了C#规则引擎RulesEngine的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-02-02
  • C#函数式编程中的惰性求值详解

    C#函数式编程中的惰性求值详解

    这篇文章主要介绍了C#函数式编程中的惰性求值详解,本文讲解了惰性求值的相关知识并给出代码实例,需要的朋友可以参考下
    2015-01-01
  • C#中ftp检测目录是否存在和创建文件夹的实现

    C#中ftp检测目录是否存在和创建文件夹的实现

    本文主要介绍了C#中ftp检测目录是否存在和创建文件夹的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-07-07
  • C#中Action和Func的区别

    C#中Action和Func的区别

    这篇文章主要介绍了C#中Action和Func的区别,是进行C#程序设计时需要加以注意的知识点,需要的朋友可以参考下
    2014-09-09
  • C#实现仿QQ抽屉式窗体的设计方法

    C#实现仿QQ抽屉式窗体的设计方法

    QQ软件对于绝大多数的人来说再熟悉不过了,它以使用方便、界面美观及功能完善而著称,本文给大家介绍了C#实现仿QQ抽屉式窗体的设计方法,主要通过使用API函数WindowFromPoint和GetParent实现仿QQ的抽屉式窗体,需要的朋友可以参考下
    2024-04-04
  • C#实现ComboBox自动匹配字符

    C#实现ComboBox自动匹配字符

    本文介绍C#如何实现ComboBox自动匹配字符1.采用CustomSource当做提示集合2. 直接使用下拉列表中的项作为匹配的集合,需要了解的朋友可以参考下
    2012-12-12

最新评论