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


相关推荐

  • python字符串替换replace函数

    python字符串替换replace函数python字符串替换replace函数replace(old,new,count)old,旧字符或字符串new,新字符或字符串count,最大替换数量,从起始位置开始计数,默认替换所有注意:replace函数替换字符串,不影响原字符串示例1:默认替换所有s1=’2019.10.30’s2=s1.replace(‘.’,’-‘)s2’2019-10-30’…

    2022年6月7日
    34
  • javascript 防止重复提交

    javascript 防止重复提交

    2021年7月22日
    60
  • Python环境搭建之OpenCV

    Python环境搭建之OpenCV一、openCV介绍OpenSourceComputerVisionLibrary.OpenCV于1999年由Intel建立,如今由WillowGarage提供支持。OpenCV是一个基于

    2022年7月5日
    24
  • js查询手机号码归属地[通俗易懂]

    js查询手机号码归属地[通俗易懂]js查询手机号码归属地

    2022年7月22日
    14
  • 红色故障码大全_图论的最短路问题

    红色故障码大全_图论的最短路问题原题链接战争中保持各个城市间的连通性非常重要。本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报。注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不改变其他城市之间的连通性,则不要发出警报。输入格式:输入在第一行给出两个整数N(0 < N ≤ 500)和M(≤ 5000),分别为城市个数(于是默认城市从0到N-1编号)和连接两城市的通路条数。随后M行,每行给出一条通路所连接的两个城市的编号,其间以1个空格分隔。在城市信息之后给出被攻占的

    2022年8月8日
    1
  • Python中“取整”的各种问题[通俗易懂]

    Python向上取整的算法一、初衷:  有时候我们分页展示数据的时候,需要计算页数。一般都是向上取整,例如counts=205pageCouts=20,pages=11页。一般的除法只是取整数部分,达不到要求。二、方法:1、通用除法:  UP(A/B)=int((A+B-1)/B)  取临界值,计算下A+B-1的范围就OK.2、Python除法:…

    2022年4月17日
    92

发表回复

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

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