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


相关推荐

  • C语言实现推箱子游戏

    C语言实现推箱子游戏很早就想过做点小游戏了,但是一直没有机会动手。今天闲来无事,动起手来。过程还是蛮顺利的,代码也不是非常难。今天给大家分享一下~一、介绍开发语言:C语言开发工具:Dev-C++5.11日期:2019年9月28日作者:ZackSock也不说太多多余的话了,先看一下效果图:游戏中的人物、箱子、墙壁、球都是字符构成的。通过wasd键移动,规则的话就是推箱子的规则,也就不多说了。二、代…

    2022年5月18日
    37
  • 记录一些问题(http状态码,IDOR漏洞,API接口,http请求方式)

    记录一些问题(http状态码,IDOR漏洞,API接口,http请求方式)在以前学习渗透的过程中遇到好多的小问题,弄得人不舒服,现在记录一下,算是加固一下,首先是一个服务器返回请求HTTP状态码的值,常遇到的200,400,404,401,403,500等等,(服务器常见一共14中状态码)2**开头的成功状态码,请求处理完成,常见的200,204,206的区别,200请求成功,并返回了实体报文,204页成功了,但是没有实体报文(也就是你浏览器空白的没有东西)205页和这个差不多,206的区别是他请求成功也给你返回了实体报文,但他这个给你返回的是你G

    2022年4月30日
    85
  • Unity 3D游戏开发学习教程

    Unity 3D游戏开发学习教程用C#用Unity3D制作游戏你会学到:您将学习3D游戏开发基础知识,以使用Unity3D引擎推进事物。到本课程结束时,他们将可以轻松制作任何类型的游戏,无论是3D还是2DMP4|视频:h264,1280×720|音频:AAC,44.1KHz,2Ch语言:英语+中英文字幕(根据原英文字幕机译更准确)|时长:87节课(11h32m)|大小解压后:5.86GB描述用Unity3D开发3D游戏《2021年》是一门结构完善的高级UnityC#课程,专为完全…

    2025年11月26日
    3
  • python插值(scipy.interpolate模块的griddata和Rbf)

    python插值(scipy.interpolate模块的griddata和Rbf)1.插值scipy.interpolateSciPy的interpolate模块提供了许多对数据进行插值运算的函数,范围涵盖简单的一维插值到复杂多维插值求解。一维插值:当样本数据变化归因于一个独立的变量时;多维插值:反之样本数据归因于多个独立变量时。注:一维插值这里就不再讲述了,主要是对二维插值的一个总结。2.interp2d()fromscipy.interpolateimportinterp2dinterp2d(x,y,z,kind=’linear’)这里有几个注意事项:

    2022年5月25日
    417
  • React 路由—基本使用「建议收藏」

    React 路由—基本使用「建议收藏」一:安装运行npmireact-router-dom安装react路由依赖项创建一个App.js根组件,并在根组件中,按需导入路由需要的三个组件HashRouter:表示路由的包裹

    2022年8月2日
    10
  • java实现10种排序算法[通俗易懂]

    java实现10种排序算法[通俗易懂]1.冒泡排序(BubbleSort)importjava.util.Arrays;//冒泡排序publicclassBubbleSort_01{ publicstaticvoidmain(String[]args){ inta[]={3,44,38,5,47,15,36,26,27,2,46,4,19,50,48}; //记录比较次数 intcount=0; //i=0,第一轮比较 for(inti=0;i<a.length-1;i

    2022年6月21日
    27

发表回复

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

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