博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper
阅读量:6986 次
发布时间:2019-06-27

本文共 3949 字,大约阅读时间需要 13 分钟。

.NET 使用 MySql.Data.dll 动态库操作MySql的帮助类--MySqlHelper

參考演示样例代码,例如以下所看到的:

/// 	/// MySql 数据库操作类	/// 	public class MySqlHelper	{		/// 		/// MysqlConnection		/// 		private static MySql.Data.MySqlClient.MySqlConnection MysqlConnection;		/// 		/// 获MySql 连接置信息		/// 		/// 
public static MySql.Data.MySqlClient.MySqlConnection GetCon() { String mysqlConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["Libor_MySql_QuoteCenter_ConnectionString"].ToString(); if (MysqlConnection == null) using (MysqlConnection = new MySql.Data.MySqlClient.MySqlConnection(mysqlConnectionString)) { }; if (MysqlConnection.State == System.Data.ConnectionState.Closed) MysqlConnection.Open(); if (MysqlConnection.State == System.Data.ConnectionState.Broken) { MysqlConnection.Close(); MysqlConnection.Open(); } return MysqlConnection; } #region 运行MySQL语句或存储过程,返回受影响的行数 /// /// 运行MySQL语句或存储过程 /// /// 命令类型 /// sql语句 /// 參数 ///
运行结果
public static int ExecuteNonQuery(CommandType type, String sqlString, MySql.Data.MySqlClient.MySqlParameter[] para) { try { using (MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand()) { com.Connection = GetCon(); com.CommandText = @sqlString; com.CommandType = type; if (para != null) com.Parameters.AddRange(para); int val = com.ExecuteNonQuery(); com.Parameters.Clear(); return val; } } catch (Exception ex) { Logger.Error("运行MySQL语句或存储过程,异常!", ex); return 0; } finally { if (MysqlConnection.State != ConnectionState.Closed) MysqlConnection.Close(); } } /// /// 运行带事务的SQL语句或存储过程 /// /// 事务 /// 命令类型 /// SQL语句 /// 參数 ///
运行结果
public static int ExecuteNonQuery(MySql.Data.MySqlClient.MySqlTransaction trans, CommandType type, String sqlString, MySql.Data.MySqlClient.MySqlParameter[] para) { try { using (MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand()) { com.Connection = MysqlConnection; com.CommandText = @sqlString; com.CommandType = type; if (para != null) com.Parameters.AddRange(para); if (trans != null) com.Transaction = trans; int val = com.ExecuteNonQuery(); com.Parameters.Clear(); return val; } } catch (Exception ex) { Logger.Error("运行MySQL语句或存储过程2,异常!", ex); return 0; } finally { if (MysqlConnection.State != ConnectionState.Closed) MysqlConnection.Close(); } } #endregion #region 运行SQL语句或存储过程,返回 DataTable /// /// 运行SQL语句或存储过程,返回 DataTable /// /// 命令类型 /// SQL语句 /// 參数 ///
运行结果
public static DataTable ExecuteReaderToDataTable(CommandType type, String sqlString, MySql.Data.MySqlClient.MySqlParameter[] para) { DataTable dt = new DataTable(); MySql.Data.MySqlClient.MySqlDataReader dr = null; try { using (MySql.Data.MySqlClient.MySqlCommand com = new MySql.Data.MySqlClient.MySqlCommand()) { com.Connection = GetCon(); com.CommandText = @sqlString; com.CommandType = type; if (para != null) com.Parameters.AddRange(para); using (dr = com.ExecuteReader(CommandBehavior.CloseConnection)) { if (dr != null) dt.Load(dr); com.Parameters.Clear(); } return dt; } } catch (Exception ex) { Logger.Error("运行SQL语句或存储过程,返回 DataTable,异常!", ex); return null; } finally { if (dr != null && !dr.IsClosed) dr.Close(); if (MysqlConnection.State != ConnectionState.Closed) MysqlConnection.Close(); } } #endregion }

特别说明:

              1、MySql.Data.dll mysql官网提供的组件,下载后加入引用到当前项目就可以使用

           2、參数化处理

               在SQLServer中參数化处理符号为"@",參数化演示样例如:

SqlParameter[] param = {                new SqlParameter("@TABLEDATA", tableData)         };
               在MySql中參数化处理符号为“?”,參数化示比如:

MySql.Data.MySqlClient.MySqlParameter[] paras = {		 new MySql.Data.MySqlClient.MySqlParameter("?LIBOR_NAME",name),         };
其它參考文章例如以下:

转载地址:http://xhcpl.baihongyu.com/

你可能感兴趣的文章
创业那些年,我们一起走过的坑
查看>>
Oracle软件的美学变迁
查看>>
HttpServlet中getAllDeclaredMethods()方法
查看>>
面试题2:二维数组中的查找
查看>>
文件上传的渐进式增强
查看>>
leetcode -- Sort Colors
查看>>
C#中使用自定义的纸张大小
查看>>
1z0-052 q209_3
查看>>
行测题哦
查看>>
JavaScript Window Navigator 浏览器本身的信息
查看>>
使用Android Ant在编译时混淆
查看>>
通过Servlet 将服务器硬盘图片 展示到浏览器
查看>>
linux_nand_driver
查看>>
语义化的软件版本号规则,你是否真的了解软件的版本号
查看>>
[通俗易懂]理解“委托”
查看>>
sdut 2162:The Android University ACM Team Selection Contest(第二届山东省省赛原题,模拟题)...
查看>>
xocodebulid 自动化打包 解决提示 ld: library not found for -lPods 问题
查看>>
LPEG
查看>>
python none,null,,,,,类型
查看>>
HDU 3360 National Treasures 奇偶匹配的最低点覆盖
查看>>