博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AccessHelper
阅读量:6151 次
发布时间:2019-06-21

本文共 6970 字,大约阅读时间需要 23 分钟。

代码:

using System;using System.Data;using System.Configuration;using System.Data.OleDb;using ahwildlife.Utils;/// /// AccessHelper 的摘要说明/// public class AccessHelper{    #region 变量    protected static OleDbConnection conn = new OleDbConnection();    protected static OleDbCommand comm = new OleDbCommand();    protected static string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";    #endregion    #region 构造函数    ///     /// 构造函数    ///     public AccessHelper()    {    }    #endregion    #region 打开数据库    ///     /// 打开数据库    ///     private static void openConnection()    {        if (conn.State == ConnectionState.Closed)        {            conn.ConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=ahwildlife.mdb;Persist Security Info=False;Jet OLEDB:Database Password=sa;";            comm.Connection = conn;            try            {                conn.Open();            }            catch (Exception ex)            {                throw new Exception(ex.Message);            }        }    }    #endregion    #region 关闭数据库    ///     /// 关闭数据库    ///     private static void closeConnection()    {        if (conn.State == ConnectionState.Open)        {            conn.Close();            conn.Dispose();            comm.Dispose();        }    }    #endregion    #region 执行sql语句    ///     /// 执行sql语句    ///     public static void ExecuteSql(string sqlstr)    {        try        {            openConnection();            comm.CommandType = CommandType.Text;            comm.CommandText = sqlstr;            comm.ExecuteNonQuery();        }        catch (Exception ex)        {            throw new Exception(ex.Message);        }        finally        {            closeConnection();        }    }    #endregion    #region 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。    ///     /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭这个对象。    ///     public static OleDbDataReader DataReader(string sqlstr)    {        OleDbDataReader dr = null;        try        {            openConnection();            comm.CommandText = sqlstr;            comm.CommandType = CommandType.Text;            dr = comm.ExecuteReader(CommandBehavior.CloseConnection);        }        catch        {            try            {                dr.Close();                closeConnection();            }            catch { }        }        return dr;    }    #endregion    #region 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭    ///     /// 返回指定sql语句的OleDbDataReader对象,使用时请注意关闭    ///     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();            }        }    }    #endregion    #region 返回指定sql语句的DataSet    ///     /// 返回指定sql语句的DataSet    ///     ///     /// 
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; } #endregion #region 返回指定sql语句的DataSet /// /// 返回指定sql语句的DataSet /// /// /// 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(); } } #endregion #region 返回指定sql语句的DataTable /// /// 返回指定sql语句的DataTable /// /// ///
public static DataTable DataTable(string sqlstr) { DataTable dt = Common.GetDataTableCache(sqlstr);//读缓存 if (dt != null) { return dt.Copy(); } else { dt = new DataTable(); OleDbDataAdapter da = new OleDbDataAdapter(); try { using (OleDbConnection conn = new OleDbConnection()) { conn.ConnectionString = connectionString; conn.Open(); using (OleDbCommand comm = new OleDbCommand()) { comm.Connection = conn; comm.CommandType = CommandType.Text; comm.CommandText = sqlstr; da.SelectCommand = comm; da.Fill(dt); } } } catch (Exception e) { throw new Exception(e.Message); } finally { closeConnection(); } Common.InsertDataTableCache(sqlstr, dt);//添加缓存 return dt.Copy(); } } #endregion #region 返回指定sql语句的DataTable /// /// 返回指定sql语句的DataTable /// 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(); } } #endregion #region 返回指定sql语句的DataView /// /// 返回指定sql语句的DataView /// /// ///
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; } #endregion}
View Code

 

转载于:https://www.cnblogs.com/s0611163/p/4022134.html

你可能感兴趣的文章
并行程序设计学习心得1——并行计算机存储
查看>>
bulk
查看>>
C++ 迭代器运算
查看>>
【支持iOS11】UITableView左滑删除自定义 - 实现多选项并使用自定义图片
查看>>
【算法笔记】多线程斐波那契数列
查看>>
java8函数式编程实例
查看>>
jqgrid滚动条宽度/列显示不全问题
查看>>
在mac OS10.10下安装 cocoapods遇到的一些问题
查看>>
css技巧
查看>>
Tyvj 1728 普通平衡树
查看>>
javascript性能优化
查看>>
多路归并排序之败者树
查看>>
java连接MySql数据库
查看>>
转:Vue keep-alive实践总结
查看>>
深入python的set和dict
查看>>
C++ 11 lambda
查看>>
Android JSON数据解析
查看>>
DEV实现日期时间效果
查看>>
java注解【转】
查看>>
centos 下安装g++
查看>>