C# MySqlHelper
1、MySql官方提供ADO.NET访问模式的MySql.Data.dll,下载地址:http://dev.mysql.com/downloads/connector/net/
2、MySqlHelper,如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using MySql.Data.MySqlClient; namespace drWm.mdal { public class MySqlHelper { //private static string connstr = @"server=localhost;uid=root;pwd=121535;database=tem;charset=utf8"; private static string connstr = @"Host=127.0.0.1;UserName=root;Password=drpassword;Database=watermonitor;Port=3406;CharSet=utf8;Allow Zero Datetime=true"; #region 执行查询语句,返回MySqlDataReader /// <summary> /// 执行查询语句,返回MySqlDataReader /// </summary> /// <param name="sqlString"></param> /// <returns></returns> public static MySqlDataReader ExecuteReader(string sqlString) { MySqlConnection connection = new MySqlConnection(connstr); MySqlCommand cmd = new MySqlCommand(sqlString, connection); MySqlDataReader myReader = null; try { connection.Open(); myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); return myReader; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw new Exception(e.Message); } finally { if (myReader == null) { cmd.Dispose(); connection.Close(); } } } #endregion #region 执行带参数的查询语句,返回MySqlDataReader /// <summary> /// 执行带参数的查询语句,返回MySqlDataReader /// </summary> /// <param name="sqlString"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static MySqlDataReader ExecuteReader(string sqlString, params MySqlParameter[] cmdParms) { MySqlConnection connection = new MySqlConnection(connstr); MySqlCommand cmd = new MySqlCommand(); MySqlDataReader myReader = null; try { PrepareCommand(cmd, connection, null, sqlString, cmdParms); myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection); cmd.Parameters.Clear(); return myReader; } catch (System.Data.SqlClient.SqlException e) { connection.Close(); throw new Exception(e.Message); } finally { if (myReader == null) { cmd.Dispose(); connection.Close(); } } } #endregion #region 执行sql语句,返回执行行数 /// <summary> /// 执行sql语句,返回执行行数 /// </summary> /// <param name="sql"></param> /// <returns></returns> public static int ExecuteSql(string sql) { using (MySqlConnection conn = new MySqlConnection(connstr)) { using (MySqlCommand cmd = new MySqlCommand(sql, conn)) { try { conn.Open(); int rows = cmd.ExecuteNonQuery(); return rows; } catch (MySql.Data.MySqlClient.MySqlException e) { conn.Close(); throw e; } finally { cmd.Dispose(); conn.Close(); } } } } #endregion #region 执行带参数的sql语句,并返回执行行数 /// <summary> /// 执行带参数的sql语句,并返回执行行数 /// </summary> /// <param name="sqlString"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static int ExecuteSql(string sqlString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connstr)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, sqlString, cmdParms); int rows = cmd.ExecuteNonQuery(); cmd.Parameters.Clear(); return rows; } catch (System.Data.SqlClient.SqlException E) { throw new Exception(E.Message); } finally { cmd.Dispose(); connection.Close(); } } } } #endregion #region 执行查询语句,返回DataSet /// <summary> /// 执行查询语句,返回DataSet /// </summary> /// <param name="sql"></param> /// <returns></returns> public static DataSet GetDataSet(string sql) { using (MySqlConnection conn = new MySqlConnection(connstr)) { DataSet ds = new DataSet(); try { conn.Open(); MySqlDataAdapter DataAdapter = new MySqlDataAdapter(sql, conn); DataAdapter.Fill(ds); } catch (Exception ex) { throw ex; } finally { conn.Close(); } return ds; } } #endregion #region 执行带参数的查询语句,返回DataSet /// <summary> /// 执行带参数的查询语句,返回DataSet /// </summary> /// <param name="sqlString"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static DataSet GetDataSet(string sqlString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connstr)) { MySqlCommand cmd = new MySqlCommand(); PrepareCommand(cmd, connection, null, sqlString, cmdParms); using (MySqlDataAdapter da = new MySqlDataAdapter(cmd)) { DataSet ds = new DataSet(); try { da.Fill(ds, "ds"); cmd.Parameters.Clear(); } catch (System.Data.SqlClient.SqlException ex) { throw new Exception(ex.Message); } finally { cmd.Dispose(); connection.Close(); } return ds; } } } #endregion #region 执行带参数的sql语句,并返回object /// <summary> /// 执行带参数的sql语句,并返回object /// </summary> /// <param name="sqlString"></param> /// <param name="cmdParms"></param> /// <returns></returns> public static object GetSingle(string sqlString, params MySqlParameter[] cmdParms) { using (MySqlConnection connection = new MySqlConnection(connstr)) { using (MySqlCommand cmd = new MySqlCommand()) { try { PrepareCommand(cmd, connection, null, sqlString, cmdParms); object obj = cmd.ExecuteScalar(); cmd.Parameters.Clear(); if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value))) { return null; } else { return obj; } } catch (System.Data.SqlClient.SqlException e) { throw new Exception(e.Message); } finally { cmd.Dispose(); connection.Close(); } } } } #endregion /// <summary> /// 执行存储过程,返回数据集 /// </summary> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>DataSet</returns> public static DataSet RunProcedureForDataSet(string storedProcName, IDataParameter[] parameters) { using (MySqlConnection connection = new MySqlConnection(connstr)) { DataSet dataSet = new DataSet(); connection.Open(); MySqlDataAdapter sqlDA = new MySqlDataAdapter(); sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters); sqlDA.Fill(dataSet); connection.Close(); return dataSet; } } /// <summary> /// 构建 SqlCommand 对象(用来返回一个结果集,而不是一个整数值) /// </summary> /// <param name="connection">数据库连接</param> /// <param name="storedProcName">存储过程名</param> /// <param name="parameters">存储过程参数</param> /// <returns>SqlCommand</returns> private static MySqlCommand BuildQueryCommand(MySqlConnection connection, string storedProcName, IDataParameter[] parameters) { MySqlCommand command = new MySqlCommand(storedProcName, connection); command.CommandType = CommandType.StoredProcedure; foreach (MySqlParameter parameter in parameters) { command.Parameters.Add(parameter); } return command; } #region 装载MySqlCommand对象 /// <summary> /// 装载MySqlCommand对象 /// </summary> private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, 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 = CommandType.Text; //cmdType; if (cmdParms != null) { foreach (MySqlParameter parm in cmdParms) { cmd.Parameters.Add(parm); } } } #endregion } }
C# MySqlHelper的更多相关文章
- MySqlHelper
package utils; import java.io.IOException; import java.sql.CallableStatement; import java.sql.Connec ...
- Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
MySqlHelper.cs代码如下: using System; using System.Collections.Generic; using System.Linq; using System. ...
- MySqlHelper、CacheHelper
MySqlHelper代码: using System; using System.Collections; using System.Collections.Generic; using Syste ...
- MysqlHelper 需要重写
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Text;u ...
- PHP简单封装MysqlHelper类
MysqlHelper.class.php 1: <?php 2: 3: /** 4: * Mysql数据帮助类 5: */ 6: class MysqlHelper 7: { 8: func ...
- 在.NET连接MySQL以及封装好的MySQLHelper.cs
1.首先上MySQL网站下驱动:http://www.mysql.com/products/connector/ 2.安装下载的安装包 3.我们在Visual Studio里创建一个Web Appli ...
- MysqlHelper类
连接方式:server=localhost;port=3306;userid=root;password=123456789;database=mysql;persist security info= ...
- 简洁的MysqlHelper
把MySqlXXX的类更改为SqlXXX就可以成为sqlHelper. 另外C#也提供了MysqlHelper和sqlHelper,用起来也挺方便的. public class MySqlHelper ...
- Python学习笔记10-Python MysqlHelper ,MySql 辅助类
自己写了一个MySql辅助类,有需要的拿走: #--encoding:utf-8-- # import MySQLdb class MySQLHelper: myVersion=0.1 def __i ...
随机推荐
- 连连看beta发布
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- Scrum会议3(Beta版本)
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- Scrum会议1
小组名称:天天向上 项目名称:连连看 参会成员:王森(Master)张金生 张政 栾骄阳 时间:2016.10.16 会议内容: 1.连连看游戏选取图片素材. 2.连连看的界面设计 3.连连看具体的功 ...
- js获取各种宽高方法
屏幕的有效宽高: window.screen.availHeightwindow.screen.availWidth 网页可见区域宽:document.body.clientWidth 网页可见区域高 ...
- 3.PHP内核探索:一次请求生命周期
我们从未手动开启过PHP的相关进程,它是随着Apache的启动而运行的.PHP通过mod_php5.so模块和Apache相连(具体说来是SAPI,即服务器应用程序编程接口). PHP总共有三个模块: ...
- 错误记录--The import XXX cannot be resolved
错误:The import XXX cannot be resolved弄了好久,import类的都没问题,但就是报错.选择project --> clean后,OK.如果还不行,删掉全部imp ...
- 使用JSONP跨域请求数据
下面代码,可以使用JSONP进行跨域请求数据,Insus.NET记录以下,以备忘记.
- 【转】说说如何使用unity Vs来进行断点调试
大家可以从这下载最新版的unity vs. UnityVs1.81下载 1. 安装unity vs.首先我们打开我们下载的unity vs.然后就会看见里面有3个文件,我们双击UnityVS 2 ...
- 我的Mac软件集
哥总算用上了mac,走上了小康了 phpstorm:个人工作中最常用的软件了 pycharm:这个写python的,和phpstorm是一家开发的 sourceTree:git 可视化工具 xcode ...
- 深入了解Windows句柄到底是什么
深入了解Windows句柄到底是什么 http://blog.csdn.net/wenzhou1219/article/details/17659485 总是有新入门的Windows程序员问我Wind ...