Access的数据操作类(带异常处理)
前两天写了SQLserver的数据操作类,今天写个Access的数据操作类。跟大家分享下,这里带了异常处理。
文章属于原创,转载请指明出处,我整理也是废了很大劲的。谢谢
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> /// DBGG 的摘要说明 /// </summary> public class DBGG { public static OleDbConnection ConnAccess; public DBGG() { // // TODO: 在此处添加构造函数逻辑 // } /// <summary> /// 建立连接字符串 /// </summary> /// <returns>OleDbConnection对象</returns> public static OleDbConnection createConnectionAccess() { OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Jet OLEDB:Database Password=aizhigala;Data Source=d:\Info_DB\db_Info.mdb;"); return con; } /// <summary> /// 执行含有参数组的SQL语句返回布尔值 /// </summary> /// <param name="SQL">SQL语句</param> /// <param name="SQLPARS">参数组可以为空,我在这里用于数据的插入</param> /// <returns>布尔值</returns> public static bool AccessCMD(string SQL, params OleDbParameter[] SQLPARS) { bool pFlag = false; ConnAccess = DBGG.createConnectionAccess(); try { ConnAccess.Open(); OleDbCommand cmd = new OleDbCommand(SQL, ConnAccess); if (SQLPARS != null) { foreach (OleDbParameter p in SQLPARS) { cmd.Parameters.Add(p); } } cmd.ExecuteNonQuery(); pFlag = true; } catch (System.Data.OleDb.OleDbException e) { throw new Exception(e.Message); } finally { ConnAccess.Close(); } return pFlag; } /// <summary> /// 执行查询语句并返回DataSet /// </summary> /// <param name="SQL">sql语句</param> /// <returns>DataSet对象</returns> public static DataSet AccessAdp_Access(string SQL) { ConnAccess = DBGG.createConnectionAccess(); try { ConnAccess.Open(); OleDbDataAdapter adp = new OleDbDataAdapter(SQL, ConnAccess); DataSet ds = new DataSet(); adp.Fill(ds); return ds; } catch (System.Data.OleDb.OleDbException e) { throw new Exception(e.Message); } finally { ConnAccess.Close(); } } /// <summary> /// 运行SQL语句返回DataReader /// </summary> /// <param name="SQL">sql语句</param> /// <returns>OleDbDataReader对象</returns> public static OleDbDataReader AccessDatatReader_Access(string SQL) { ConnAccess = DBGG.createConnectionAccess(); ConnAccess.Open(); OleDbCommand Cmd = new OleDbCommand(SQL, ConnAccess); OleDbDataReader Dr; try { Dr = Cmd.ExecuteReader(CommandBehavior.CloseConnection); } catch { throw new Exception(SQL); } Cmd.Dispose(); return Dr; } /// <summary> /// 检验是否存在数据* /// </summary> /// <param name="SQL">sql语句</param> /// <returns>布尔值</returns> public static bool AccessExistRecorder(string SQL) { bool pFlag = false; ConnAccess = DBGG.createConnectionAccess(); try { ConnAccess.Open(); OleDbCommand cmd = new OleDbCommand(SQL, ConnAccess); OleDbDataReader Dr = cmd.ExecuteReader(); if (Dr.Read()) { pFlag = true; } } catch { } finally { ConnAccess.Close(); } return pFlag; } /// <summary> /// 返回SQL语句执行结果的第一行第一列 /// </summary> /// <param name="SQL">sql语句</param> /// <returns>字符串</returns> public string ReturnFirstValue(string SQL) { string str; DataSet ds = DBGG.AccessAdp_Access(SQL); str = ds.Tables[0].Rows[0][0].ToString(); return str; } }