zoukankan      html  css  js  c++  java
  • 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

    }
  • 相关阅读:
    Linux下架构高可用性网络----HA+LB+lvs
    MacBook如何用Parallels Desktop安装windows7/8
    Win10如何彻底禁用小娜?彻底禁用小娜的方法
    安卓手机微信发不出去怎么办 微信不能发信息怎么办
    计算机名、主机名、用户账户名与NetBIOS名有什么区别
    安装corel x8提示你已安装了另外一个版本
    ssh整合问题总结--在添加商品模块实现图片(文件)的上传
    代理设计模式之静态代理与动态代理(超..)详解
    Java基础--反射机制的知识点梳理
    ssh整合问题总结--运行项目时报java.lang.StackOverflowError(堆栈溢出)异常
  • 原文地址:https://www.cnblogs.com/bluedy1229/p/1314925.html
Copyright © 2011-2022 走看看