C#实现操作MySql数据层类MysqlHelper实例
程序员文章站
2022-10-25 19:03:00
本文实例讲述了c#实现操作mysql数据层类mysqlhelper。分享给大家供大家参考。具体如下:
using system;
using system.da...
本文实例讲述了c#实现操作mysql数据层类mysqlhelper。分享给大家供大家参考。具体如下:
using system; using system.data; using system.configuration; using system.collections.generic; using system.linq; using system.text; using system.xml.linq; using mysql.data; using mysql.data.mysqlclient; namespace videoworks.itmanage.dal { public abstract class mysqlhelper { //数据库连接字符串 public static string conn = "database='device_manage';data source='localhost';user id='root';password='123456';charset='utf8';pooling=true;allow zero datetime=true"; /// <summary> /// 给定连接的数据库用假设参数执行一个sql命令(不返回数据集) /// </summary> /// <param name="connectionstring">一个有效的连接字符串</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>执行命令所影响的行数</returns> public static int executenonquery(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); using (mysqlconnection conn = new mysqlconnection(connectionstring)) { preparecommand(cmd, conn, null, cmdtype, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } } /// <summary> /// 用现有的数据库连接执行一个sql命令(不返回数据集) /// </summary> /// <param name="connection">一个现有的数据库连接</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>执行命令所影响的行数</returns> public static int executenonquery(mysqlconnection connection, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, connection, null, cmdtype, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } /// <summary> ///使用现有的sql事务执行一个sql命令(不返回数据集) /// </summary> /// <remarks> ///举例: /// int result = executenonquery(connstring, commandtype.storedprocedure, "publishorders", new mysqlparameter("@prodid", 24)); /// </remarks> /// <param name="trans">一个现有的事务</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>执行命令所影响的行数</returns> public static int executenonquery(mysqltransaction trans, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, trans.connection, trans, cmdtype, cmdtext, commandparameters); int val = cmd.executenonquery(); cmd.parameters.clear(); return val; } /// <summary> /// 用执行的数据库连接执行一个返回数据集的sql命令 /// </summary> /// <remarks> /// 举例: /// mysqldatareader r = executereader(connstring, commandtype.storedprocedure, "publishorders", new mysqlparameter("@prodid", 24)); /// </remarks> /// <param name="connectionstring">一个有效的连接字符串</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>包含结果的读取器</returns> public static mysqldatareader executereader(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { //创建一个mysqlcommand对象 mysqlcommand cmd = new mysqlcommand(); //创建一个mysqlconnection对象 mysqlconnection conn = new mysqlconnection(connectionstring); //在这里我们用一个try/catch结构执行sql文本命令/存储过程,因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, //因此commandbehaviour.closeconnection 就不会执行 try { //调用 preparecommand 方法,对 mysqlcommand 对象设置参数 preparecommand(cmd, conn, null, cmdtype, cmdtext, commandparameters); //调用 mysqlcommand 的 executereader 方法 mysqldatareader reader = cmd.executereader(commandbehavior.closeconnection); //清除参数 cmd.parameters.clear(); return reader; } catch { //关闭连接,抛出异常 conn.close(); throw; } } /// <summary> /// 返回dataset /// </summary> /// <param name="connectionstring">一个有效的连接字符串</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns></returns> public static dataset getdataset(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { //创建一个mysqlcommand对象 mysqlcommand cmd = new mysqlcommand(); //创建一个mysqlconnection对象 mysqlconnection conn = new mysqlconnection(connectionstring); //在这里我们用一个try/catch结构执行sql文本命令/存储过程, //因为如果这个方法产生一个异常我们要关闭连接,因为没有读取器存在, try { //调用 preparecommand 方法,对 mysqlcommand 对象设置参数 preparecommand(cmd, conn, null, cmdtype, cmdtext, commandparameters); //调用 mysqlcommand 的 executereader 方法 mysqldataadapter adapter = new mysqldataadapter(); adapter.selectcommand = cmd; dataset ds = new dataset(); adapter.fill(ds); //清除参数 cmd.parameters.clear(); conn.close(); return ds; } catch (exception e) { throw e; } } /// <summary> /// 用指定的数据库连接字符串执行一个命令并返回一个数据集的第一列 /// </summary> /// <remarks> ///例如: /// object obj = executescalar(connstring, commandtype.storedprocedure, "publishorders", new mysqlparameter("@prodid", 24)); /// </remarks> ///<param name="connectionstring">一个有效的连接字符串</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>用 convert.to{type}把类型转换为想要的 </returns> public static object executescalar(string connectionstring, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); using (mysqlconnection connection = new mysqlconnection(connectionstring)) { preparecommand(cmd, connection, null, cmdtype, cmdtext, commandparameters); object val = cmd.executescalar(); cmd.parameters.clear(); return val; } } /// <summary> /// 用指定的数据库连接执行一个命令并返回一个数据集的第一列 /// </summary> /// <remarks> /// 例如: /// object obj = executescalar(connstring, commandtype.storedprocedure, "publishorders", new mysqlparameter("@prodid", 24)); /// </remarks> /// <param name="connection">一个存在的数据库连接</param> /// <param name="cmdtype">命令类型(存储过程, 文本, 等等)</param> /// <param name="cmdtext">存储过程名称或者sql命令语句</param> /// <param name="commandparameters">执行命令所用参数的集合</param> /// <returns>用 convert.to{type}把类型转换为想要的 </returns> public static object executescalar(mysqlconnection connection, commandtype cmdtype, string cmdtext, params mysqlparameter[] commandparameters) { mysqlcommand cmd = new mysqlcommand(); preparecommand(cmd, connection, null, cmdtype, cmdtext, commandparameters); object val = cmd.executescalar(); cmd.parameters.clear(); return val; } /// <summary> /// 准备执行一个命令 /// </summary> /// <param name="cmd">sql命令</param> /// <param name="conn">oledb连接</param> /// <param name="trans">oledb事务</param> /// <param name="cmdtype">命令类型例如 存储过程或者文本</param> /// <param name="cmdtext">命令文本,例如:select * from products</param> /// <param name="cmdparms">执行命令的参数</param> private static void preparecommand(mysqlcommand cmd, mysqlconnection conn, mysqltransaction trans, commandtype cmdtype, string cmdtext, mysqlparameter[] cmdparms) { if (conn.state != connectionstate.open) conn.open(); cmd.connection = conn; cmd.commandtext = cmdtext; if (trans != null) cmd.transaction = trans; cmd.commandtype = cmdtype; if (cmdparms != null) { foreach (mysqlparameter parm in cmdparms) cmd.parameters.add(parm); } } } }
希望本文所述对大家的c#程序设计有所帮助。
上一篇: Java web 修改默认web部署路径
下一篇: C#使用shell32获取文件属性的方法