SoFunction
Updated on 2025-03-08

C# database operation class AccessHelper instance

using System;
using ;
using ;
using ;
using ;


/// <summary>
/// Summary description of AccessHelper
/// </summary>
public class AccessHelper
{
#region variable
    protected static OleDbConnection conn = new OleDbConnection();
    protected static OleDbCommand comm = new OleDbCommand();
    protected static string connectionString = @"Provider=.4.0;Data Source=;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
    #endregion

#region constructor
    /// <summary>
/// Constructor
    /// </summary>
    public AccessHelper()
    {

    }
    #endregion

#region Open the database
    /// <summary>
/// Open the database
    /// </summary>
    private static void openConnection()
    {
        if ( == )
        {
            = @"Provider=.4.0;Data Source=;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
            = conn;
            try
            {
                ();
            }
            catch (Exception ex)
            {
                throw new Exception();
            }
        }
    }
    #endregion

#region Close the database
    /// <summary>
/// Close the database
    /// </summary>
    private static void closeConnection()
    {
        if ( == )
        {
            ();
            ();
            ();
        }
    }
    #endregion

#region Execute SQL statements
    /// <summary>
/// Execute sql statements
    /// </summary>
    public static void ExecuteSql(string sqlstr)
    {
        try
        {
            openConnection();
            = ;
            = sqlstr;
            ();
        }
        catch (Exception ex)
        {
            throw new Exception();
        }
        finally
        {
            closeConnection();
        }
    }
    #endregion

#region Returns the OleDbDataReader object of the specified sql statement. Please be careful to close this object when using it.
    /// <summary>
/// Returns the OleDbDataReader object of the specified sql statement. Please be careful to close this object when using it.
    /// </summary>
    public static OleDbDataReader DataReader(string sqlstr)
    {
        OleDbDataReader dr = null;
        try
        {
            openConnection();
            = sqlstr;
            = ;

            dr = ();
        }
        catch
        {
            try
            {
                ();
                closeConnection();
            }
            catch { }
        }
        return dr;
    }
    #endregion

#region Returns the OleDbDataReader object of the specified sql statement. Please be careful to close it when using it.
    /// <summary>
/// Return the OleDbDataReader object of the specified sql statement. Please be careful to close it when using it.
    /// </summary>
    public static void DataReader(string sqlstr, ref OleDbDataReader dr)
    {
        try
        {
            openConnection();
            = sqlstr;
            = ;
            dr = ();
        }
        catch
        {
            try
            {
                if (dr != null && !)
                    ();
            }
            catch
            {
            }
            finally
            {
                closeConnection();
            }
        }
    }
    #endregion

#region Returns the DataSet of the specified sql statement
    /// <summary>
/// Returns the DataSet of the specified sql statement
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    public static DataSet DataSet(string sqlstr)
    {
        DataSet ds = new DataSet();
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            = ;
            = sqlstr;
            = comm;
            (ds);

        }
        catch (Exception e)
        {
            throw new Exception();
        }
        finally
        {
            closeConnection();
        }
        return ds;
    }
    #endregion

#region Returns the DataSet of the specified sql statement
    /// <summary>
/// Returns the DataSet of the specified sql statement
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <param name="ds"></param>
    public static void DataSet(string sqlstr, ref DataSet ds)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            = ;
            = sqlstr;
            = comm;
            (ds);
        }
        catch (Exception e)
        {
            throw new Exception();
        }
        finally
        {
            closeConnection();
        }
    }
    #endregion

#region Returns the DataTable of the specified SQL statement
    /// <summary>
/// Returns the DataTable of the specified SQL statement
    /// </summary>
    /// <param name="sqlstr"></param>
    /// <returns></returns>
    public static DataTable DataTable(string sqlstr)
    {
DataTable dt = (sqlstr);//Read cache
        if (dt != null)
        {
            return ();
        }
        else
        {
            dt = new DataTable();
            OleDbDataAdapter da = new OleDbDataAdapter();
            try
            {
                using (OleDbConnection conn = new OleDbConnection())
                {
                    = connectionString;
                    ();
                    using (OleDbCommand comm = new OleDbCommand())
                    {
                        = conn;
                        = ;
                        = sqlstr;
                        = comm;
                        (dt);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception();
            }
            finally
            {
                closeConnection();
            }
(sqlstr, dt);//Add cache
            return ();
        }
    }
    #endregion

#region Returns the DataTable of the specified SQL statement
    /// <summary>
/// Returns the DataTable of the specified SQL statement
    /// </summary>
    public static void DataTable(string sqlstr, ref DataTable dt)
    {
        OleDbDataAdapter da = new OleDbDataAdapter();
        try
        {
            openConnection();
            = ;
            = sqlstr;
            = comm;
            (dt);
        }
        catch (Exception e)
        {
            throw new Exception();
        }
        finally
        {
            closeConnection();
        }
    }
    #endregion

#region Returns the DataView of the specified sql statement
    /// <summary>
/// Returns the DataView of the specified sql statement
    /// </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();
            = ;
            = sqlstr;
            = comm;
            (ds);
            dv = [0].DefaultView;
        }
        catch (Exception e)
        {
            throw new Exception();
        }
        finally
        {
            closeConnection();
        }
        return dv;
    }
    #endregion
}