C#操作Access通用类实例
程序员文章站
2023-12-03 11:58:16
本文实例讲述了c#操作access通用类。分享给大家供大家参考。具体如下:
using system;
using system.data;
using sy...
本文实例讲述了c#操作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; using system.collections; /// <summary> /// accehelper 的摘要说明 /// </summary> public static class accesshelper { //数据库连接字符串 public static readonly string conn = "provider=microsoft.jet.oledb.4.0;data source=" + httpcontext.current.request.physicalapplicationpath + system.configuration.configurationmanager.connectionstrings["connectionstring"].connectionstring; // 用于缓存参数的hash表 private static hashtable parmcache = hashtable.synchronized(new hashtable()); /// <summary> /// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集) /// </summary> /// <param name="connectionstring">一个有效的连接字符串</param> /// <param name="commandtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>执行命令所影响的行数</returns> public static int executenonquery(string connectionstring, string cmdtext, params oledbparameter[] commandparameters) { oledbcommand cmd = new oledbcommand(); using (oledbconnection conn = new oledbconnection(connectionstring)) { preparecommand(cmd, conn, null, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } } /// <summary> /// 用现有的数据库连接执行一个sql命令(不返回数据集) /// </summary> /// <remarks> ///举例: /// int result = executenonquery(connstring, "publishorders", new oledbparameter("@prodid", 24)); /// </remarks> /// <param name="conn">一个现有的数据库连接</param> /// <param name="commandtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>执行命令所影响的行数</returns> public static int executenonquery(oledbconnection connection, string cmdtext, params oledbparameter[] commandparameters) { oledbcommand cmd = new oledbcommand(); preparecommand(cmd, connection, null, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } /// <summary> ///使用现有的sql事务执行一个sql命令(不返回数据集) /// </summary> /// <remarks> ///举例: /// int result = executenonquery(trans, "publishorders", new oledbparameter("@prodid", 24)); /// </remarks> /// <param name="trans">一个现有的事务</param> /// <param name="commandtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>执行命令所影响的行数</returns> public static int executenonquery(oledbtransaction trans, string cmdtext, params oledbparameter[] commandparameters) { oledbcommand cmd = new oledbcommand(); preparecommand(cmd, trans.connection, trans, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } /// <summary> /// 用执行的数据库连接执行一个返回数据集的sql命令 /// </summary> /// <remarks> /// 举例: /// oledbdatareader r = executereader(connstring, "publishorders", new oledbparameter("@prodid", 24)); /// </remarks> /// <param name="connectionstring">一个有效的连接字符串</param> /// <param name="commandtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>包含结果的读取器</returns> public static oledbdatareader executereader(string connectionstring, string cmdtext, params oledbparameter[] commandparameters) { //创建一个sqlcommand对象 oledbcommand cmd = new oledbcommand(); //创建一个sqlconnection对象 oledbconnection conn = new oledbconnection(connectionstring); //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, //因此commandbehaviour.closeconnection 就不会执行 try { //调用 preparecommand 方法,对 sqlcommand 对象设置参数 preparecommand(cmd, conn, null, cmdtext, commandparameters); //调用 sqlcommand 的 executereader 方法 oledbdatareader reader = cmd.executereader(commandbehavior.closeconnection); //清除参数 cmd.parameters.clear(); return reader; } catch { //关闭连接,抛出异常 conn.close(); throw; } } /// <summary> /// 返回一个dataset数据集 /// </summary> /// <param name="connectionstring">一个有效的连接字符串</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>包含结果的数据集</returns> public static dataset executedataset(string connectionstring, string cmdtext, params oledbparameter[] commandparameters) { //创建一个sqlcommand对象,并对其进行初始化 oledbcommand cmd = new oledbcommand(); using (oledbconnection conn = new oledbconnection(connectionstring)) { preparecommand(cmd, conn, null, cmdtext, commandparameters); //创建sqldataadapter对象以及dataset oledbdataadapter da = new oledbdataadapter(cmd); dataset ds = new dataset(); try { //填充ds da.fill(ds); // 清除cmd的参数集合 cmd.parameters.clear(); //返回ds return ds; } catch { //关闭连接,抛出异常 conn.close(); throw; } } } /// <summary> /// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列 /// </summary> /// <remarks> ///例如: /// object obj = executescalar(connstring, "publishorders", new oledbparameter("@prodid", 24)); /// </remarks> ///<param name="connectionstring">一个有效的连接字符串</param> /// <param name="commandtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>用 convert.to{type}把类型转换为想要的 </returns> public static object executescalar(string connectionstring, string cmdtext, params oledbparameter[] commandparameters) { oledbcommand cmd = new oledbcommand(); using (oledbconnection connection = new oledbconnection(connectionstring)) { preparecommand(cmd, connection, null, cmdtext, commandparameters); object val = cmd.executescalar(); cmd.parameters.clear(); return val; } } /// <summary> /// 用指定的数据库连接执行一个命令并返回一个数据集的第一列 /// </summary> /// <remarks> /// 例如: /// object obj = executescalar(connstring, "publishorders", new oledbparameter("@prodid", 24)); /// </remarks> /// <param name="conn">一个存在的数据库连接</param> /// <param name="commandtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>用 convert.to{type}把类型转换为想要的 </returns> public static object executescalar(oledbconnection connection, string cmdtext, params oledbparameter[] commandparameters) { oledbcommand cmd = new oledbcommand(); preparecommand(cmd, connection, null, cmdtext, commandparameters); object val = cmd.executescalar(); cmd.parameters.clear(); return val; } /// <summary> /// 将参数集合添加到缓存 /// </summary> /// <param name="cachekey">添加到缓存的变量</param> /// <param name="cmdparms">一个将要添加到缓存的sql参数集合</param> public static void cacheparameters(string cachekey, params oledbparameter[] commandparameters) { parmcache[cachekey] = commandparameters; } /// <summary> /// 找回缓存参数集合 /// </summary> /// <param name="cachekey">用于找回参数的关键字</param> /// <returns>缓存的参数集合</returns> public static oledbparameter[] getcachedparameters(string cachekey) { oledbparameter[] cachedparms = (oledbparameter[])parmcache[cachekey]; if (cachedparms == null) return null; oledbparameter[] clonedparms = new oledbparameter[cachedparms.length]; for (int i = 0, j = cachedparms.length; i < j; i++) clonedparms =(oledbparameter[])((icloneable)cachedparms).clone(); return clonedparms; } /// <summary> /// 准备执行一个命令 /// </summary> /// <param name="cmd">sql命令</param> /// <param name="conn">sql连接</param> /// <param name="trans">sql事务</param> /// <param name="cmdtext">命令文本,例如:select * from products</param> /// <param name="cmdparms">执行命令的参数</param> private static void preparecommand(oledbcommand cmd, oledbconnection conn, oledbtransaction trans, string cmdtext, oledbparameter[] cmdparms) { //判断连接的状态。如果是关闭状态,则打开 if (conn.state != connectionstate.open) conn.open(); //cmd属性赋值 cmd.connection = conn; cmd.commandtext = cmdtext; //是否需要用到事务处理 if (trans != null) cmd.transaction = trans; cmd.commandtype = commandtype.text; //添加cmd需要的存储过程参数 if (cmdparms != null) { foreach (oledbparameter parm in cmdparms) cmd.parameters.add(parm); } } }
希望本文所述对大家的c#程序设计有所帮助。