微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

AccessHelper

代码

using System;
 System.Data;
 System.Configuration;
 System.Data.OleDb;
 ahwildlife.Utils;


/// <summary>
/// AccessHelper 的摘要说明
</summary>
public class AccessHelper
{
    #region 变量
    protected static OleDbConnection conn = new OleDbConnection();
    static OleDbCommand comm =  OleDbCommand();
    static string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";
    #endregion

    #region 构造函数
    <summary>
     构造函数
    </summary>
    public AccessHelper()
    {

    }
    #region 打开数据库
     打开数据库
    private void openConnection()
    {
        if (conn.State == ConnectionState.Closed)
        {
            conn.ConnectionString = ;
            comm.Connection = conn;
            try
            {
                conn.open();
            }
            catch (Exception ex)
            {
                throw  Exception(ex.Message);
            }
        }
    }
    #region 关闭数据库
     关闭数据库
     closeConnection()
    {
         ConnectionState.Open)
        {
            conn.Close();
            conn.dispose();
            comm.dispose();
        }
    }
    #region 执行sql语句
     执行sql语句
    void Executesql(string sqlstr)
    {
        
        {
            openConnection();
            comm.CommandType = CommandType.Text;
            comm.CommandText = sqlstr;
            comm.ExecuteNonQuery();
        }
         (Exception ex)
        {
             Exception(ex.Message);
        }
        finally
        {
            closeConnection();
        }
    }
    #region 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
     返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。
    static OleDbDataReader DataReader( sqlstr)
    {
        OleDbDataReader dr = null;
        
        {
            openConnection();
            comm.CommandText = sqlstr;
            comm.CommandType = CommandType.Text;

            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);
        }
        
        {
            
            {
                dr.Close();
                closeConnection();
            }
             { }
        }
        return dr;
    }
    关闭
    关闭
    void DataReader(string sqlstr,ref OleDbDataReader dr)
    {
         CommandType.Text;
            dr =
            {
                if (dr != null && !dr.IsClosed)
                    dr.Close();
            }
            
            {
            }
            
            {
                closeConnection();
            }
        }
    }
    #region 返回指定sql语句的DataSet
     返回指定sql语句的DataSet
    </summary>
    <param name="sqlstr"></param>
    <returns></returns>
    static DataSet DataSet( sqlstr)
    {
        DataSet ds =  DataSet();
        OleDbDataAdapter da =  OleDbDataAdapter();
         sqlstr;
            da.SelectCommand = comm;
            da.Fill(ds);

        }
         (Exception e)
        {
             Exception(e.Message);
        }
        
        {
            closeConnection();
        }
         ds;
    }
    <param name="ds"></param>
    void DataSet( DataSet ds)
    {
        OleDbDataAdapter da =  comm;
            da.Fill(ds);
        }
        #region 返回指定sql语句的DataTable
     返回指定sql语句的DataTable
    static DataTable DataTable( sqlstr)
    {
        DataTable dt = Common.GetDataTableCache(sqlstr);//读缓存
        if (dt != )
        {
             dt.copy();
        }
        else
        {
            dt =  DataTable();
            OleDbDataAdapter da =  OleDbDataAdapter();
            using (OleDbConnection conn =  OleDbConnection())
                {
                    conn.ConnectionString = connectionString;
                    conn.open();
                    using (OleDbCommand comm =  OleDbCommand())
                    {
                        comm.Connection = conn;
                        comm.CommandType = CommandType.Text;
                        comm.CommandText = sqlstr;
                        da.SelectCommand = comm;
                        da.Fill(dt);
                    }
                }
            }
             (Exception e)
            {
                 Exception(e.Message);
            }
            
            {
                closeConnection();
            }
            Common.InsertDataTableCache(sqlstr,dt);添加缓存
             dt.copy();
        }
    }
    void DataTable( DataTable dt)
    {
        OleDbDataAdapter da =  comm;
            da.Fill(dt);
        }
        #region 返回指定sql语句的DataView
     返回指定sql语句的DataView
    static DataView DataView( sqlstr)
    {
        OleDbDataAdapter da =  OleDbDataAdapter();
        DataView dv =  DataView();
        DataSet ds =  DataSet();
         comm;
            da.Fill(ds);
            dv = ds.Tables[0].defaultview;
        }
         dv;
    }
    #endregion

}
View Code

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐