Access Helper similar sqlhelper good stuff

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>
/// AccessHelper 的摘要说明
/// </summary>
public class AccessHelper
{
    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();

    AccessHelper public ()
{
   //
   // the TODO: Add constructor logic here
   //
}

    /// <summary>
    /// 打开数据库
    /// </summary>
    private static void openConnection()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.ConnectionString = @"Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + HttpContext.Current.Server.MapPath(ConfigurationManager.AppSettings["myconn"]);
            comm.Connection = conn;
            try
            {
                conn.Open();
            }
            catch (Exception e)
            { throw new Exception(e.Message); }

        }

    }
    /// <summary>
    /// 关闭数据库
    /// </summary>
    private static void closeConnection()
    {
        if (conn.State == ConnectionState.Open)
        {
            conn.Close();
            conn.Dispose();
            comm.Dispose();
        }
    }
    /// <summary>
    /// 执行sql语句
    /// </summary>
    /// <param name="sqlstr"></param>
    public static void excuteSql(string sqlstr)
    {
        try
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery ();
        }
        the catch (Exception E)
        {
            the throw new new Exception (e.Message);
        }
        the finally
        }; {closeConnection ()
    }
    /// <Summary>
    When /// Returns sql statement OleDbDataReader objects, using Please note close this object.
    /// </ Summary>
    /// <param name = "sqlstr"> </ param>
    /// <Returns> </ Returns>
    public static dataReader the OleDbDataReader (String sqlstr)
    {
        the OleDbDataReader DR = null;
        the try
        {
            the openConnection ( );
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;

            comm.ExecuteReader = DR (the CommandBehavior.CloseConnection);
        }
        the catch
        {
            the try
            {
                dr.Close ();
                closeConnection ();
            }
            the catch {}
        }
        return DR;
    }
    /// <Summary>
    /// Returns sql statement OleDbDataReader objects, note off using
    /// </ Summary>
    /// <param name = "sqlstr"> </ param>
    /// <param name = "DR"> </ param>
    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();
            }
        }
    }
    /// <summary>
    /// 返回指定sql语句的dataset
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// 返回指定sql语句的dataset
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="ds"></param>
    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();
        }
    }
    /// <summary>
    /// 返回指定sql语句的datatable
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    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;
    }
    /// <summary>
    /// 返回指定sql语句的datatable
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="dt"></param>
    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();
        }
    }
    /// <summary>
    /// 返回指定sql语句的dataview
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    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;
    }
}

Reproduced in: https: //www.cnblogs.com/majinyu/archive/2008/08/01/1258337.html

Guess you like

Origin blog.csdn.net/weixin_34121304/article/details/93666460