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#数据库模型示例

    支持多类型数据库的c#数据库模型示例

    本文为大家提供一个c#数据库访问模型,支持多类型数据库,简单抽取数据库访问函数,大家参考使用吧
    2014-01-01
  • C#生成随机字符串的实例

    C#生成随机字符串的实例

    本文介绍了“C#生成随机字符串的实例”,需要的朋友可以参考一下
    2013-03-03
  • C#创建不规则窗体的4种方式详解

    C#创建不规则窗体的4种方式详解

    在这里我们将实现的是C#创建不规则窗体的几种方式,包括自定义窗体,不规则图形等等。希望对大家有所帮助。
    2015-10-10
  • 深入了解C#多线程安全

    深入了解C#多线程安全

    使用多线程无法避免的一个问题就是多线程安全。那什么是多线程安全?如何解决多线程安全?本文将通过一些简单的例子为大家详细介绍一下多线程相关的问题,感兴趣的可以了解一下
    2021-12-12
  • asp.net之生成验证码的方法集锦(一)

    asp.net之生成验证码的方法集锦(一)

    现在很多网站都有注册登录的页面,为了更好的满足用户体验和网站的安全性,很多网站都采用动态生成的图形码或者是附加码进行验证,这篇文章主要就是介绍生成验证码的方法,需要的朋友可以参考下
    2015-08-08
  • C#自定义特性(Attribute)详解

    C#自定义特性(Attribute)详解

    本文详细讲解了C#的自定义特性(Attribute),文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#实现的一款比较美观的验证码完整实例

    C#实现的一款比较美观的验证码完整实例

    这篇文章主要介绍了C#实现的一款比较美观的验证码,以完整实例形式分析了C#生成验证码与前端调用验证码的实现技巧,需要的朋友可以参考下
    2016-06-06
  • winform dateTime数据类型转换方法

    winform dateTime数据类型转换方法

    这篇文章主要介绍了winform dateTime数据类型转换方法,需要的朋友可以参考下
    2017-02-02
  • C#操作INI文件的示例代码

    C#操作INI文件的示例代码

    这篇文章主要为大家详细介绍了C#操作INI文件的相关知识,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随上班一起学习一下
    2023-12-12
  • C#编程实现动态改变配置文件信息的方法

    C#编程实现动态改变配置文件信息的方法

    这篇文章主要介绍了C#编程实现动态改变配置文件信息的方法,涉及C#针对xml格式文件的相关操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2016-06-06

最新评论