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

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

/// <summary>/// MySql 数据库操作类/// </summary>public class MySqlHelper{/// <summary>/// MysqlConnection/// </summary>private static MySql.Data.MySqlClient.MySqlConnection MysqlConnection;/// <summary>/// 获MySql 连接置信息/// </summary>/// <returns></returns>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语句或存储过程,返回受影响的行数/// <summary>/// 运行MySQL语句或存储过程/// </summary>/// <param name="type">命令类型</param>/// <param name="sqlString">sql语句</param>/// <param name="pstmt">參数</param>/// <returns>运行结果</returns>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();}}/// <summary>/// 运行带事务的SQL语句或存储过程/// </summary>/// <param name="trans">事务</param>/// <param name="type">命令类型</param>/// <param name="sqlString">SQL语句</param>/// <param name="pstmt">參数</param>/// <returns>运行结果</returns>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/// <summary>/// 运行SQL语句或存储过程,返回 DataTable/// </summary>/// <param name="type">命令类型</param>/// <param name="sqlString">SQL语句</param>/// <param name="pstmt">參数</param>/// <returns>运行结果</returns>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://www.jb51.net/article/30342.htm

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

  1. mysql c 驱动dll_C#调用MySQL数据库(使用MySql.Data.dll连接)mysql-connector-net-6.10.4.msi

    下载地址:http://dev.mysql.com/downloads/connector/net/ 安装指导 1.安装:mysql-connector-net-6.10.4.msi 其下载地址:ht ...

  2. 探索MySql.Data.dll

    ADO.NET Driver for MySQL  MySql.Data.dll是.Net访问MySQL数据库的一个驱动,完全ADO.NET数据访问模式,由MySQL官方提供,有多个版本可选择. 最早 ...

  3. wpf 执行mysql命令_WPF(.net 3.5)使用MySql.Data.dll进行mysql数据库操作

    开发环境 win764 vs2015 wpf+.net3.5 下载 MySql.Data.dll 引用到项目中 看好版本 另一个版本 为什么放两个版本呢 因为写这篇文章的时候用的是5.2.3的版本,但 ...

  4. delphi 用MDAC微软数据库访问组件的dll动态库

    delphi 用MDAC微软数据库访问组件的dll动态库 一.概念 MDAC(Microsoft Data Access Components)是微软数据库访问组件,Netpise和许多利用数据库的软 ...

  5. Winform中使用Mysql.Data.dll实现连接Mysql数据库并执行sql语句(排除ddl等非法语句的执行)

    场景 Winform中连接Mysql8并查询表中数据进行显示: Winform中连接Mysql8并查询表中数据进行显示_BADAO_LIUMANG_QIZHI的博客-CSDN博客 与上面实现的流程类似 ...

  6. 【Unity导入MySql.Data.dll报错】

    Unity导入MySql.Data.dll报错 错误的起因,想用Unity对MySQL操作.操作参考了勤诚勇毅的帖子 链接: https://blog.csdn.net/qq_43333567/art ...

  7. 在 windows 上如何根据 dll 动态库生成 lib 文件?

    在 windows 系统平台上,dll 动态库没有提供 .lib 文件,又不想动态获取函数地址怎么办? 使用 lib.exe 工具可以根据 .def 定义文件生成 .lib 文件. 例如,我这里有一个 ...

  8. Windows系统下通过JNI调用dll动态库的实现

    目的:java代码使用jni获取数据 工具: eclipse + Microsoft visual studio (c++) 业务代码: Java代码业务实现: package com.weip.jn ...

  9. C#使用EPPlus.dll动态库在一般处理程序中实现将datatable导出到excel

    一..如何安装EPPLus 1.在Visual Studio的解决方案的引用上右键选择管理NeGet程序包 2.在管理NuGet程序包中搜索EPPlus,点击进行安装,安装后便直接添加到引用了 二.在 ...

最新文章

  1. centos7.0 lamp mysql_CentOS7 yum安装LNMP以及LAMP
  2. 如何删除tmp计算机桌面,Win10系统中tmp文件删除不了应该如何解决?
  3. python循环语句打印矩形_Python中使用循环语句打印三角形、菱形
  4. 工业机器人电柜布线_沙湾附近回收工业锅炉“本信息长期有效”
  5. java 鼠标绘图,教您如何用JAVA程序实现鼠标绘图
  6. 搭建 LEGO EV3 的 PyCharm Python 开发环境
  7. hibernate继承映射之每个具体类一张表
  8. php 电压 异常,tv断线警告是什么原因
  9. leetcode初级算法4.只出现一次的数字
  10. jsp源码oracle数据库,JSP与oracle数据库交互案例
  11. STL模板之vector与sort的使用
  12. datagrip 自动展示库名_DataGrip 2018.1.4 功能强大的多引擎数据库管理工具
  13. Redis(RedisTemplate)运算、算法(incr、decr、increment)
  14. access 更新整列数据_创建和运行更新查询
  15. 115.不同的子序列
  16. NOIP2020退役记
  17. 坦克大战源代码java_Java版坦克大战游戏源码示例
  18. SpringBoot日志
  19. 批处理文件——多个QQ一键登录
  20. 三项式与组合数(lucas板子)

热门文章

  1. 【生活随想】实习结束以及开始校园招聘
  2. BGP相邻体之间磋商的过程
  3. JAVA的正则表达式语法
  4. 做出的C++选择以及背后的原因
  5. SSH 组建轻量级架构 附录 -- 遇到的问题和解答
  6. Oracle procedure
  7. PHP的学习--PHP的引用
  8. SAS、R以及SPSS的比较__统计语言大战
  9. Windows Phone 8初学者开发—第19部分:设置RecordAudio.xaml页面
  10. USACO Section 1.5 Checker Challenge