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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 网站用户单点登录系统

    1背景
      在网站建设的过程中,多个应用系统一般是在不同的时期开发完成的。各应用系统由于功能侧重、设计方法和开发技术有所不同,也就形成了各自独立的用户库和用户认证体系。随着网站的发展,会出现这样的用户群体:以其中的一个用户为例,他(她)使用网站的多个应用系统,但在每个应用系统中有独立的账号,没有一个整体上的网站用户账号的概念,进入每一个应用系统前都需要以该应用系统的账号来登录。这带给用户不方便的使用感受,用户会想:既然我使用的是同一个网站上的应用,为什么不能在一次在网站上

    2022年4月13日
    89
  • mybatis框架–学习笔记(上)

    mybatis框架–学习笔记(上)

    2021年9月26日
    41
  • Java项目开发文档(javaweb实战项目)

    项目开发过程中为了增加程序的可读性和程序的健壮性,方便后期程序的调试和维护,所以需要在开发过程中统一技术规范,一般会在项目初期确定好相关文档作为这一统一的规范。不同公司会对文档做不同要求,划不同的分类,但一般来说(或者拿自己的经验说)大致可以分为需求文档、接口文档、流程图(可以单独作为一份文件可以作为附件附在文档中)、变更文件等。一、需求文档在项目启动之后,项目的目标已经明确了,那么就要

    2022年4月15日
    132
  • android 置灰不可点击,Android Studio 运行按钮灰色的完美解决方法「建议收藏」

    android 置灰不可点击,Android Studio 运行按钮灰色的完美解决方法「建议收藏」AndroidStudio运行按钮灰色的完美解决方法今天新建项目的时候突然发现编译后运行按钮为灰色。解决方案:第一步:点击图中的AddConfiguration,出来如下界面第二步:点+号,并选择AndroidApp选项出来下图所示界面第三步:在Module中下拉框中选择app如果在Module下拉框没有app这个选项点击搜索框,输入sync,从搜索结果中选择如下项:点击运行…

    2022年8月28日
    1
  • php 除法取两位小数,php中除法取整的方法(round,ceil,floor)「建议收藏」

    php 除法取两位小数,php中除法取整的方法(round,ceil,floor)「建议收藏」PHP中遇到需要将除法所得结果取整的情况时,就需要用到以下方法:1.round:四舍五入round()函数对浮点数进行四舍五入。语法:round(x,prec)参数描述x可选。规定要舍入的数字。prec可选。规定小数点后的位数。说明:返回将x根据指定精度prec(十进制小数点后数字的数目)进行四舍五入的结果。prec也可以是负数或零(默认值)。提示:PHP默认不能正确处理类似”…

    2022年6月21日
    49
  • 图神经网络(GNN)的简介「建议收藏」

    近年来,图神经网络(GNN)在社交网络、知识图、推荐系统甚至生命科学等各个领域得到了越来越广泛的应用。GNN在对图节点之间依赖关系进行建模的强大功能,使得与图分析相关的研究领域取得了突破。本文介绍了图神经网络的基本原理,以及两种高级的算法,DeepWalk和GraphSage。图(Graph)在讨论GNN之前,我们先来了解一下什么是图。在计算机科学中,图是由顶点和边两部分组成的一种数据结构…

    2022年4月18日
    220

发表回复

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

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