ACCESS数据库访问类封装

 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.IO;
using System.Data.OleDb;


/// <summary>
/// AccessDB 的摘要说明
/// </summary>
public class AccessDB
{
 public AccessDB()
 {
  //
  // TODO: 在此处添加构造函数逻辑
  //
    }
   // ORStr
    private readonly string constr = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ORStr"].ToString();//得到数据库联接(判断是什么联接)

    public int ExecuteSQL(string sql)
    {
        int result = -1;
        using (OleDbConnection conn = new OleDbConnection(constr))
        {

            OleDbCommand cmd = new OleDbCommand(sql, conn);
            conn.Open();
            result = cmd.ExecuteNonQuery();
            conn.Close();
        }
        return result;
    }

    /// <summary>
    /// 查询操作
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public System.Data.DataSet QuerySQL(string sql, string tableName)
    {
        DataSet ds = null;
        OleDbDataAdapter dsa = null;
        OleDbConnection conn = null;
        try
        {

            {
                conn = new OleDbConnection(constr);

                dsa = new OleDbDataAdapter(sql, conn);

                ds = new DataSet();
                conn.Open();
                dsa.Fill(ds, tableName);
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            if (conn != null)
            {
                conn.Close();
            }
        }
        return ds;
    }

    System.Data.IDataReader ExecuteReader(string sql)
    {
        IDataReader dr = null;
        return dr;
    }

 

    #region IDBCommon 成员


    public int ExecuteSQL(string sql, params IDbDataParameter[] parameters)
    {
        int result = -1;
        if (parameters != null)
        {
            using (OleDbConnection conn = new OleDbConnection(constr))
            {
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                foreach (IDbDataParameter para in parameters)
                {
                    cmd.Parameters.Add(para);
                }
                conn.Open();
                result = cmd.ExecuteNonQuery();
                conn.Close();
            }
        }
        else
        {

            using (OleDbConnection conn = new OleDbConnection(constr))
            {
                OleDbCommand cmd = new OleDbCommand(sql, conn);
                conn.Open();
                result = cmd.ExecuteNonQuery();
                conn.Close();
            }

        }
        return result;
    }

    #endregion

    #region IDBCommon 成员
    /// <summary>
    /// 打开连接
    /// </summary>
    /// <param name="strconn">连接字符串</param>
    /// <param name="conn"></param>
    /// <returns>执行是否成功</returns>

    public int OpenConn(string strconn, IDbConnection conn)
    {
        int falge = -1;
        try
        {
            if (conn != null)
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            conn = new OleDbConnection(strconn);
            conn.Open();//打开连接
            falge = 1;
        }
        catch (Exception ex)
        {
            falge = -1;
            throw new Exception("_数据库连接失败_:" + ex.Message);//抛出错误信息
        }
        return falge;
    }
    #endregion

 

  }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值