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


相关推荐

  • win10 系统WLAN不见了,win10找不到wifi选项的解决办法

    win10 系统WLAN不见了,win10找不到wifi选项的解决办法问题如下图所示:可以看到没有WLAN设置的选项而且是无网络的状态解决办法:两步就好了第一步,以管理员身份运行CMD点击Cortana输入cmd鼠标右键命令提示符以管理员身份运行还有一种办法就是进入C:\Windows\System32还是右键以管理员身份运行两种方法都可以第二步:使用netshwinsockreset…

    2022年7月27日
  • LBS定位技术「建议收藏」

    LBS定位技术「建议收藏」http://www.cnblogs.com/LBSer/p/3295642.htmlLBS定位技术从方法上可分成三类:基于三角关系的定位技术、基于场景分析的定位技术、基于临近关系的定位技术(唐毅和杨

    2022年8月2日
  • JAVA正则表达式:Pattern类与Matcher类详解(转)

    JAVA正则表达式:Pattern类与Matcher类详解(转)java.util.regex是一个用正则表达式所订制的模式来对字符串进行匹配工作的类库包。它包括两个类:Pattern和MatcherPattern一个Pattern是一个正则表达式经编译后的表

    2022年7月1日
  • 秒杀多线程第九篇 经典线程同步总结 关键段 事件 互斥量 信号量

    秒杀多线程第九篇 经典线程同步总结 关键段 事件 互斥量 信号量前面《秒杀多线程第四篇一个经典的多线程同步问题》提出了一个经典的多线程同步互斥问题,这个问题包括了主线程与子线程的同步,子线程间的互斥,是一道非常经典的多线程同步互斥问题范例,后面分别用了四篇《秒杀多线程第五篇经典线程同步关键段CS》《秒杀多线程第六篇经典线程同步事件Event》《秒杀多线程第七篇经典线程同步互斥量Mutex》《秒杀多线程第八篇经典线程同步信号量Semaphore》来详细介绍常用的

    2022年7月15日
  • 了解你的敌人:跟踪僵尸网络

    了解你的敌人:跟踪僵尸网络了解你的敌人:跟踪僵尸网络 利用蜜网对僵尸主机了解更多 蜜网项目组 & 蜜网研究联盟http://www .honeynet.org最后修改日期 : 2005 年 3 月 13 日 翻译者artemis:吴智发密罐是一种用来发现攻击工具,攻击策略与攻击者攻击动机的知名技术。在本文中,我们考虑一种特殊的安全威胁:运行僵尸网络的个人与组织。僵尸网络是一个可以由攻击者远程控制的已被攻陷主机组成的网络。由

    2022年7月25日
  • ASP.Net中menu控件使用「建议收藏」

    ASP.Net中menu控件使用「建议收藏」为了使用方便asp.net中提供了两类控件html控件与用户控件。这里不在详细介绍。menu属于用户控件。menu最重要的是item属性,item包含了menu控件要显示的类表,几级菜单神马的,点击item属性后面出现一个对话框,也可以点menu控件的箭头如下图:点击编辑菜单项也会弹出对话框,根据自己爱好可以进行设定,下面是我自己设定的:当然旁边对应的是每个条目对应的属

    2022年7月27日

发表回复

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

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