.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. 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. php笔记08:数据库编程---使用php的MySQL扩展库操作MySQL数据库

    1.使用php的MySQL扩展库操作MySQL数据库: php有3种方式操作MySQL数据库 (1)mysql扩展库 (2)mysqli扩展库 (3)pdo     mysql扩展库与mysql数据库 ...

  3. mysqli扩展库操作mysql数据库

    配置环境 配置php.ini文件让php支持mysqli扩展库 extension=php_mysqli.dll 建库建表 详见博客 “mysql扩展库操作mysql数据库” 查询数据库 <?p ...

  4. Silverlight用户无法注册之MySql.Data.dll不一致

    本篇博文记录本人解决一个"用户无法注册"的问题的过程 1 问题描述: 用户可以删除:但不能注册,也不能修改权限: 图 1用户注册失败 图 2修改用户权限出错 2 解决过程 1)粗暴 ...

  5. Eclipse下生成.dll动态库及.a静态库使用 for Windows [z]

    以后的主要工作就是做库了,将我们的C或者C++写的接口做成库,给客户端使用,因此有必要知道库的使用和制作方法.主要是在Eclipse下搞了搞,公司用的是Carbide,也差不多.库做好了,用SVN已提 ...

  6. 关于Linux和Windows下部署mysql.data.dll的注册问题

    mysql ado.net connector下载地址: http://dev.mysql.com/downloads/connector/net/ 选择版本: Generally Available ...

  7. 使用Mysql.data.dll文件在服务器上运行访问Mysql

    我使用的这个Mysql.data.dll文件 web.config上面主要需要声明以下代码 <system.data> <DbProviderFactories> <ad ...

  8. Java调用dll动态库

    最近项目里使用java调用dll动态库,因此研究了一下这方面的东西. 使用的工具包如下 <dependency> <groupId>net.java.dev.jna</g ...

  9. MySql.Data.dll官网下载

    Mysql.Data.dll官网下载 在项目开发中链接MySQL数据库经常要用到Mysql.Data.dll,网上虽然有很多,但是还是比较信赖官网的 今天就从官网下载一次记录一下过程 1.下载地址 官 ...

随机推荐

  1. 与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知

    原文:与众不同 windows phone (9) - Push Notification(推送通知)之概述, 推送 Toast 通知 [索引页][源码下载] 与众不同 windows phone ( ...

  2. jquery.form.js用法之清空form的方法

    本段代码摘取自jquery.form.js中,由于觉得该方法的使用性非常强,同时也可独立拿出来使用.该段代码言简意赅可以很好的作为学习参考. /** * Clears the form data. T ...

  3. RPC 的概念模型与实现解析(转)

    今天分布式应用.云计算.微服务大行其道,作为其技术基石之一的 RPC 你了解多少?一篇 RPC 的技术总结文章,数了下 5k+ 字,略长,可能也不适合休闲的碎片化时间阅读,可以先收藏抽空再细读:) 全 ...

  4. Mysql RR隔离更新列没有索引 会锁全表

    <pre name="code" class="html">mysql> show variables like '%tx_isolation ...

  5. Linux多线程服务端编程:使用muduo C++网络库

    内容推荐本 书主要讲述采用现代C++在x86-64 Linux上编写多线程TCP网络服务程序的主流常规技术,重点讲解一种适应性较强的多线程服务器的编程模型,即one loop per thread.这 ...

  6. boost 的函数式编程库 Phoenix入门学习

    这篇文章是我学习boost phoenix的总结. 序言 Phoenix是一个C++的函数式编程(function programming)库.Phoenix的函数式编程是构建在函数对象上的.因此,了 ...

  7. ZipHelper 压缩和解压帮助类

    ZipHelper 压缩和解压帮助类 关于本文档的说明 本文档基于ICSharpCode.SharpZipLib.dll的封装,常用的解压和压缩方法都已经涵盖在内,都是经过项目实战积累下来的 欢迎传播 ...

  8. 开源搜索引擎评估:lucene sphinx elasticsearch

    开源搜索引擎评估:lucene sphinx elasticsearch 开源搜索引擎程序有3大类 lucene系,java开发,包括solr和elasticsearch sphinx,c++开发,简 ...

  9. ONVIFclient搜索设备获取rtsp解决开发笔记(精华文章)

    总结:           眼下ONVIF协议系列设备已经超过一半的数字监控行业占据更多,关闭,作为一个开发者,你还在犹豫下就明白了?本文介绍了ONVIFclient从搜索,认证,获取,媒体信息获取. ...

  10. mysql导出和导入数据库

    出口 在dos计划,切换到mysql按照该文件夹bin下一个.输入以下命令 mysqldump -u root -p nxu_life > nxu_life2.sql 运行完毕后,就能够看到在b ...