Access 通用数据访问类(asp.net 2.0 c#)

Access 通用数据访问类(asp.net 2.0 c#)

仿照以前收集的一个经典sql server数据访问类,稍做修改。
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;

/// <summary>
/// DataAccess 的摘要说明
/// </summary>
public class DataAccess
{

    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();
 public DataAccess()
 {

  //init
 }
    private static void openConnection()
    {

        if (conn.State == ConnectionState.Closed)
        {

            conn.ConnectionString = @”Provider=Microsoft.Jet.OleDb.4.0;Data Source=”+ConfigurationManager.AppSettings[“myconn”];//web.config文件里设定。
            comm.Connection = conn;
            try
            {

                conn.Open();
            }
            catch (Exception e)
            { throw new Exception(e.Message); }

        }
      
    }//打开数据库
 
    private static void closeConnection()
    {

        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
    }//关闭数据库

    public static void excuteSql(string sqlstr)
    {

        try
        {

            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
        }
        catch (Exception e)
        {

            throw new Exception(e.Message);
        }
        finally
        { closeConnection(); }
    }//执行sql语句

    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;
        }//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
    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();
            }
        }
    }//返回指定sql语句的OleDbDataReader对象,使用时请注意关闭

    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;
    }//返回指定sql语句的dataset

    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();
        }
    }//返回指定sql语句的dataset

    public static DataTable dataTable(string sqlstr)
    {

        DataTable dt = new DataTable();
        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();
        }
        return dt;
    }//返回指定sql语句的datatable
    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();
        }
    }//返回指定sql语句的datatable

    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;
    }
//返回指定sql语句的dataview

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/111080.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ABAP开发语言「建议收藏」

    ABAP开发语言「建议收藏」2.第二部分ABAP开发语言2.1.ABAP基础2.1.1.语言概述2.1.1.1.程序结构ABAP程序源码结构包括数据定义和处理块两部分;处理块又分为事件块,对话模块,过程。过程中可以定义自己的局部变量。事件块,对话模块,只能使用全局数据定义。2.1.1.2.程序类型可直接运行的应用程序(可分配事务代码)可执行程序Executableprogram,类型代码…

    2025年6月21日
    2
  • AD域的详细介绍「建议收藏」

    AD域的详细介绍「建议收藏」文章目录1、什么是域2、内网的环境:3、域的组成:4、域的部署域账号登录成员机的过程:组策略GPO(GroupPolicy)1、什么是域Domain:域是计算机网络的一种形式,其中所有用户账户,计算机,打印机和其他安全主体都在位于称为域控制器的一个或多个中央计算机集群上的中央数据库中注册。两个域之间可以通过建立信任(Trust)关系来进行联系2、内网的环境:1)工作组:默认模式,人人平等,但是不方便管理2)域:人人不平等,优点:可以实现集中管理、统一管理3、域的组成:1)域控制器(DC:D

    2022年5月13日
    72
  • swoole 查看tcp开启进程数

    swoole 查看tcp开启进程数

    2022年2月12日
    48
  • pycharm设置远程调试_调试助听器需要什么配置的电脑

    pycharm设置远程调试_调试助听器需要什么配置的电脑条件pycharm需要专业版方式使用远程解释器 使用远程调试器使用远程解释器默认情况下我们在本地开发Python程序时,使用的是本地的Python解释器,如果你安装了virtualenv或者pyenv的话,还可以选择这些虚拟环境。而使用Pycharm的专业版,则还可以选择使用远程Linux机器上的解释器。下面就来介绍下使用远程解释器的步骤。 远程部署配置远程部署主要用…

    2022年8月29日
    2
  • linux目录结构详解_linux目录的结构及含义

    linux目录结构详解_linux目录的结构及含义前言平常linux系统用的也不少,那么linux下的每个目录都是用来干什么的,小伙伴们有仔细研究过吗?让我们来了解下吧Linux系统目录结构登录系统后,在当前命令窗口下输入命令:[root@

    2022年7月31日
    6
  • Python sum() TypeError: ‘int‘ object is not callable xxxxxxxxx XXXXXXXXXX

    Python sum() TypeError: ‘int‘ object is not callable xxxxxxxxx XXXXXXXXXXPythonsum()TypeError:’int’objectisnotcallablexxxxxxxxxXXXXXXXXXX代码中定义了sum变量,导致sum()方法异常。

    2022年8月22日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号