.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. 领略TApplicationEvents的风采

    这是它的声明,它的数据成员全部都是Event,而没有真正意义上的数据(如此一来,几乎可以猜测,它本身什么都做不了): TCustomApplicationEvents = class(TCompone ...

  2. 根据图像路径,创建CBitmap对象的方法

    因为项目的关系,需要根据图像路径,创建CBitmap对象.起初查资料找到了LoadBitmap这个函数,根据CSDN得 BOOL LoadBitmap ( LPCTSTR lpszResourceNa ...

  3. C++ operator overload -- 操作符重载

    C++ operator overload -- 操作符重载 2011-12-13 14:18:29 分类: C/C++ 操作符重载有两种方式,一是以成员函数方式重载,另一种是全局函数. 先看例子 # ...

  4. myBatis 基础测试 表关联关系配置 集合 测试

    myBatis 基础测试 表关联关系配置 集合 测试 测试myelipse项目源码 sql 下载 http://download.csdn.net/detail/liangrui1988/599388 ...

  5. ExtJs4 笔记(3) Ext.Ajax 对ajax的支持

    本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持,包括Ajax封装,函数事件操作封装,还有扩展的常用函数等.Ajax服务端交互式操作是提交到.NET MVC.后续服务端交互都采用这一方式实现. ...

  6. 11 款最好 CSS 框架 让你的网站独领风骚

    网页设计和发展领域已经成为竞争激烈的虚拟世界.想要在网络的虚拟世界中生存,仅有一堆静止的在线网络应用是远远不够的,网页必须要有很多功能,配以让人无法抗拒的设计.网页编码一定要合适.精确,才能保证不发生 ...

  7. Mysql ODBC 5.1 Driver免安装脚本

    在使用Mysql 的时候,需要使用ODBC数据源的方式来连接mysql,所以常常需要用到免安装的驱动,自己参考官网的脚本, 修改了一个实用点的脚本,放出来大家一起分享: 安装mysql odbc 5. ...

  8. 简体中文 — ANSI Common Lisp 中文版

    简体中文 - ANSI Common Lisp 中文版 简体中文¶

  9. Redis 学习笔记五 经常使用php函数

    PHPRedis的安装在这里: http://blog.csdn.net/xundh/article/details/46288277 键值操作 $redis = new Redis(); $redi ...

  10. 阿录帮帮忙—spring mvc 的hello world

    一:web.xml配置 <!-- Spring MVC配置 --> <servlet> <servlet-name>Spring MVC Dispatcher Se ...