今意外看见一贴子谈oledbhelper,突然想起自已一直用的由sqlhelper类转成的oledbhelper,在此分享,自己用了二年了,还没发现异常, 代码如下: using system; using system.data; using system.data.oledb; using system.configuration; using system.colle
今意外看见一贴子谈oledbhelper,突然想起自已一直用的由sqlhelper类转成的oledbhelper,在此分享,自己用了二年了,还没发现异常, 代码如下:
using system;using system.data;using system.data.oledb;using system.configuration;using system.collections;using system.data.sql;using system.text;namespace lihui.common{ /// /// summary description for oledbhelper /// public class oledbhelper { //database connection strings public static readonly string conn_string = configurationmanager.appsettings[oledbconnectionstring]; public static readonly string conn_string1 = configurationmanager.appsettings[oledbconnectionstring1]; // hashtable to store cached parameters private static hashtable parmcache = hashtable.synchronized(new hashtable()); #region =executenonquery= public static int executenonquery(string connstring, commandtype cmdtype, string cmdtext) { return executenonquery(connstring, cmdtype, cmdtext, null); } public static int executenonquery(oledbconnection conn, commandtype cmdtype, string cmdtext) { return executenonquery(conn, cmdtype, cmdtext, null); } public static int executenonquery(oledbtransaction trans, commandtype cmdtype, string cmdtext) { return executenonquery(trans, cmdtype, cmdtext, null); } public static int executenonquery(string connstring, commandtype cmdtype, string cmdtext, params oledbparameter[] cmdparms) { oledbcommand cmd = new oledbcommand(); using (oledbconnection conn = new oledbconnection(connstring)) { preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms); int val = cmd.executenonquery(); //清除cmd的参数 cmd.parameters.clear(); if (conn.state == connectionstate.open) { conn.close(); } return val; } } public static int executenonquery(oledbconnection conn, commandtype cmdtype, string cmdtext, params oledbparameter[] cmdparms) { oledbcommand cmd = new oledbcommand(); preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms); int val = cmd.executenonquery(); cmd.parameters.clear(); if (conn.state == connectionstate.open) { conn.close(); } return val; } public static int executenonquery(oledbtransaction trans, commandtype cmdtype, string cmdtext, params oledbparameter[] cmdparms) { oledbcommand cmd = new oledbcommand(); preparecommand(cmd, trans.connection, trans, cmdtype, cmdtext, cmdparms); int val = cmd.executenonquery(); cmd.parameters.clear(); if (cmd.connection.state == connectionstate.open) { cmd.connection.close(); } return val; } #endregion #region =executereader= public static oledbdatareader executereader(string connectionstring, commandtype commandtype, string commandtext) { //pass through the call providing null for the set of oledbparameters return executereader(connectionstring, commandtype, commandtext, (oledbparameter[])null); } public static oledbdatareader executereader(string connstring, commandtype cmdtype, string cmdtext, params oledbparameter[] cmdparms) { oledbcommand cmd = new oledbcommand(); oledbconnection conn = new oledbconnection(connstring); try { preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms); oledbdatareader rdr = cmd.executereader(commandbehavior.closeconnection); cmd.parameters.clear(); if (conn.state == connectionstate.open) { conn.close(); } return rdr; } catch { conn.close(); throw; } } #endregion #region =executedataset= public static dataset executedataset(string connectionstring, commandtype commandtype, string commandtext) { return executedataset(connectionstring, commandtype, commandtext, (oledbparameter[])null); } public static dataset executedataset(string connectionstring, commandtype commandtype, string commandtext, params oledbparameter[] commandparameters) { using (oledbconnection cn = new oledbconnection(connectionstring)) { cn.open(); //调用重载方法 return executedataset(cn, commandtype, commandtext, commandparameters); } } public static dataset executedataset(oledbconnection connection, commandtype commandtype, string commandtext) { return executedataset(connection, commandtype, commandtext, (oledbparameter[])null); } public static dataset executedataset(oledbconnection connection, commandtype commandtype, string commandtext, params oledbparameter[] commandparameters) { //创建一个oledbcommand对象,并对其进行初始化 oledbcommand cmd = new oledbcommand(); preparecommand(cmd, connection, (oledbtransaction)null, commandtype, commandtext, commandparameters); //创建oledbdataadapter对象以及dataset oledbdataadapter da = new oledbdataadapter(cmd); dataset ds = new dataset(); //填充ds da.fill(ds); // 清除cmd的参数集合 cmd.parameters.clear(); if (cmd.connection.state == connectionstate.open) { cmd.connection.close(); } //返回ds return ds; } #endregion #region =executedatatable= public static datatable executedatatable(string connectionstring, commandtype commandtype, string commandtext) { return executedatatable(connectionstring, commandtype, commandtext, (oledbparameter[])null); } public static datatable executedatatable(string connectionstring, commandtype commandtype, string commandtext, params oledbparameter[] commandparameters) { using (oledbconnection cn = new oledbconnection(connectionstring)) { cn.open(); //调用重载方法 return executedatatable(cn, commandtype, commandtext, commandparameters); } } public static datatable executedatatable(oledbconnection connection, commandtype commandtype, string commandtext) { return executedatatable(connection, commandtype, commandtext, (oledbparameter[])null); } public static datatable executedatatable(oledbconnection connection, commandtype commandtype, string commandtext, params oledbparameter[] commandparameters) { //创建一个oledbcommand对象,并对其进行初始化 oledbcommand cmd = new oledbcommand(); preparecommand(cmd, connection, (oledbtransaction)null, commandtype, commandtext, commandparameters); //创建oledbdataadapter对象以及dataset oledbdataadapter da = new oledbdataadapter(cmd); dataset ds = new dataset(); //填充ds da.fill(ds); // 清除cmd的参数集合 cmd.parameters.clear(); if (cmd.connection.state == connectionstate.open) { cmd.connection.close(); } //返回ds return ds.tables[0]; } #endregion #region =executescalar= public static object executescalar(string connstring, commandtype cmdtype, string cmdtext) { return executescalar(connstring, cmdtype, cmdtext, null); } public static object executescalar(string connstring, commandtype cmdtype, string cmdtext, params oledbparameter[] cmdparms) { oledbcommand cmd = new oledbcommand(); using (oledbconnection conn = new oledbconnection(connstring)) { preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms); object val = cmd.executescalar(); cmd.parameters.clear(); if (conn.state == connectionstate.open) { conn.close(); } return val; } } public static object executescalar(oledbconnection conn, commandtype cmdtype, string cmdtext) { return executescalar(conn, cmdtype, cmdtext, null); } public static object executescalar(oledbconnection conn, commandtype cmdtype, string cmdtext, params oledbparameter[] cmdparms) { oledbcommand cmd = new oledbcommand(); preparecommand(cmd, conn, null, cmdtype, cmdtext, cmdparms); object val = cmd.executescalar(); cmd.parameters.clear(); if (conn.state == connectionstate.open) { conn.close(); } return val; } #endregion public static void cacheparameters(string cachekey, params oledbparameter[] cmdparms) { parmcache[cachekey] = cmdparms; } 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 clonedparms[i] = (oledbparameter)((icloneable)cachedparms[i]).clone(); return clonedparms; } public static void preparecommand(oledbcommand cmd, oledbconnection conn, oledbtransaction trans, commandtype cmdtype, 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 = cmdtype; //添加cmd需要的存储过程参数 if (cmdparms != null) { foreach (oledbparameter parm in cmdparms) cmd.parameters.add(parm); } } }} 使用方法跟sqlhelper类相似。