C# · 12月 19, 2021

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