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)
上一篇 2021年7月24日 下午10:00
下一篇 2021年7月25日 上午8:00


相关推荐

  • C币商城帮助文档「建议收藏」

    C币商城帮助文档「建议收藏」帮助中心您需要什么样的帮助?关于C币商城:C币商城是为了奖励在CSDN平台有共享精神的用户。如果您曾在博客频道发表博客分享您的经验,或者在论坛为他人解惑,或者在下载频道上传优质的资源,或者在CSDN其他任一平台。温馨提示:如需更多帮助,请发邮件至webmaster#csdn.net(发送时请把地址中的‘#’换成‘@’)或拨打电话:400-660-0108常见问题关于发…

    2022年6月4日
    39
  • LaTeX的安装教程(Texlive 2020 + TeX studio)

    LaTeX的安装教程(Texlive 2020 + TeX studio)LaTeX安装步骤1、TexLive安装1.1下载TexLive1.2安装TexLive1、TexLive安装1.1下载TexLive点击TexLive使用清华镜像进行下载。如下图所示。1.2安装TexLive1.2.1打开下载后的.ISO文件,如下图所示。以管理员身份运行install-tl-windows.bat文件。1.2.2运行后的界面如下图所示。软件的安装路径默认为C盘,这里可以修改为其他磁盘。1.2.3修改好软件的安装路径后,点击Advanced,

    2022年6月11日
    54
  • 智能体数据清理:AI Agents for Beginners存储优化技术

    智能体数据清理:AI Agents for Beginners存储优化技术

    2026年3月16日
    3
  • 生成树协议STP、RSTP和MSTP原理的理解

    生成树协议STP、RSTP和MSTP原理的理解在组网中 很可能会出现环路或者称冗余链路 为避免形成广播风暴 需要一种方法来避免形成环路 并且在主链路故障中断时候 可以将冗余链路自动切换为转发状态 以恢复网络的连通性 生成树协议 STP RSTP MSTP 就可以实现这样的功能 STP 802 1D SpanningTree 生成树协议 基本思想将网络的拓扑修剪为树形拓扑 拓扑图形状与一棵树相似 这样就不会

    2026年3月19日
    3
  • SpringBoot整合dubbo详述「建议收藏」

    环境:jdk1.8+springboot-2.2.1+dubbo-2.5.3+zookeeper-3.5.5+maven构建工具:IDEA我们知道,dubbo的本质就是一个远程服务调用的分布式框架。为了演示分布式架构的大致流程,我们项目里首先要建立对应的角色节点,即服务提供者、消费者以及公共接口服务,总体结构为:dubbo_demo:是总项目duboo-api:主要是…

    2022年4月16日
    53
  • stm32蓝牙模块控制小车_51单片机蓝牙控制小车

    stm32蓝牙模块控制小车_51单片机蓝牙控制小车STM32库函数开发系列文章目录第一篇:STM32F103ZET6单片机双串口互发程序设计与实现第二篇:最简单DIY基于STM32单片机的蓝牙智能小车设计方案文章目录STM32库函数开发系列文章目录前言一、最简单DIY基于STM32单片机的蓝牙智能小车设计方案是什么?二、使用步骤1.准备硬件2.准备一个串口通信的代码3.修改源码三、运行与调试总结前言    daodanjishui物联网核心原创技术之最简单DIY基于STM32单片机的蓝牙智能小车设计方案。    市面上有各种开源STM3

    2022年10月10日
    5

发表回复

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

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