OleDb 数据库通用类

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.Collections;
using System.Collections.Specialized;
using System.Data.SqlClient;
using System.Data.OleDb;

 

/// <summary>
/// DBConnection 的摘要说明
/// </summary>
public class DBConnection
{
    protected static string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.Web.HttpContext.Current.Server.MapPath("/aspnet/App_Data/student.mdb");//ACCESS链接字符串
    public static OleDbConnection conn = new OleDbConnection(strConn);//OleDb链接类的实例化
 public DBConnection()
 {
    }
    public static OleDbConnection Db_Conn() 
    {
        if (conn.State != ConnectionState.Open)
        { conn.Open(); }
         return conn;

    }
    public static OleDbDataReader ExecuteReader(string CommandTxt)  //ExecuteReader
    {
        try
        {
            OleDbCommand Commd = new  OleDbCommand(CommandTxt, conn);
            Commd.CommandType= CommandType.Text;
            OleDbDataReader Dr;
            Db_Conn();
            Dr = Commd.ExecuteReader();
            Commd.Dispose();
            return Dr;
        }
        catch (System.Data.SqlClient.SqlException e)
        { conn.Close(); throw new Exception(e.Message); }

        finally { }

    }
    public void ExecuteNonQuery(string CommandTxt)
    {
        try
        {
            using (OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn))
            {
                Db_Conn();
                OleDbCommd.ExecuteNonQuery();
                OleDbCommd.Dispose();
            }
        }
        catch (Exception ex)
        { throw ex; }
        finally
        { conn.Close(); }
    }
    public static void DataBind(System.Web.UI.WebControls.GridView MYGridView, string CommandTxt)
    {
        try
        {
            OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn);
            OleDbCommd.CommandType = CommandType.Text;
            OleDbDataReader Dr;
            Db_Conn();
            Dr = OleDbCommd.ExecuteReader();
            MYGridView.DataSource = Dr;
            MYGridView.DataBind();
            Dr.Close();
            OleDbCommd.Dispose();
            close();
        }
        catch (Exception ex)
        { close(); throw ex; }

    }
    public static void DataBind(System.Web.UI.WebControls.DataList MyDataList, string CommandTxt)
    {
        try
        {
            OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn);
            OleDbCommd.CommandType = CommandType.Text;
            OleDbDataReader Dr;
            Db_Conn();
            Dr = OleDbCommd.ExecuteReader();
            MyDataList.DataSource = Dr;
            MyDataList.DataBind();
            Dr.Close();
            OleDbCommd.Dispose();
            close();
        }
        catch (Exception ex)
        { close(); throw ex; }

    }
    public static void DataBind(System.Web.UI.WebControls.Repeater MyRepeater, string CommandTxt)
    {
        try
        {
            OleDbCommand OleDbCommd = new OleDbCommand(CommandTxt, conn);
            OleDbCommd.CommandType = CommandType.Text;
            OleDbDataReader Dr;
            Db_Conn();
            Dr = OleDbCommd.ExecuteReader();
            MyRepeater.DataSource = Dr;
            MyRepeater.DataBind();
            Dr.Close();
            OleDbCommd.Dispose();
            close();
        }
        catch (Exception ex)
        { close(); throw ex; }
    }

    public static DataSet FillDataSet(string sql)
    {
        DataSet ds = new DataSet();
        try
        {
            Db_Conn();

            using (OleDbCommand Commd = new  OleDbCommand(sql, conn))
            {
                OleDbDataAdapter adapter = new OleDbDataAdapter(Commd);
                adapter.Fill(ds);
                return ds;
            }
        }
        catch (Exception Ex)
        { throw Ex; }
        finally
        { close(); }

    }
    public static int GetRowsNum(string sqlstr)  //判断几个记录
    {
        DataSet ds = FillDataSet(sqlstr);
        return ds.Tables[0].Rows.Count;
    }
    public static bool IfRead(string CommandTxt)
    {
        try
        {
            if (GetRowsNum(CommandTxt) == 0)
            { return false; }
            else
            { return true; }

        }
        catch (Exception Ex)
        { throw Ex; }
        finally {  }

     
    }
    public static void close()
    {
        if (conn.State != ConnectionState.Closed)
        { conn.Close(); }

    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值