.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. 积累的VC编程小技巧之视图

    1.如何得到视图指针 [问题提出]    现在你有一个多线程的Demo,你想在多线程里处理视图指针里的函数,我们给这个函数起个名字:Put();该如何实现呢?   //有两种方法可以实现你的要求:   ...

  2. linux c正则

    c 正则 --------------------------------------------------    标准的C和C++都不支持正则表达式,但有一些函数库可以辅助C/C++程序员完成这一 ...

  3. Swift - 使用UIWebView和UIToolbar制作一个浏览器

    使用网页控件(UIWebView)与工具栏控件(UIToolbar),我们可以自制一个小型的浏览器,其功能如下: 1,输入网址,点击“Go”按钮加载网页 2,加载过程中有进度条,同时可以点击停止按钮取 ...

  4. inline与lnk2001、lnk2019,鸡肋?

    inline函数居然出现了lnk2001.lnk2019,先贴代码. a.h #pragma once class A { public: inline void foo();     void us ...

  5. [IDEs]Eclipse For Mac , 常用快捷键

    Cmd + O:  查看.java中得方法,变量,等结构 Cmd + T:     查看继承关系 Cmd + K:          查找下一个选中的成员 Cmd + E:  查看已经打开的文件 Cm ...

  6. drupal THEME主要文件

    **.info 文件** .info 文件是一个必需的文件:Drupal 必须包括它,才干看到主题. .info 文件告诉 Drupal 主题的内部名称.比如,假设这个文件的名称是 ibmtheme. ...

  7. [Python] heapq简介

    [Python] heapq简介 « Lonely Coder [Python] heapq简介 judezhan 发布于 2012 年 8 月 8 日 暂无评论 发表评论 假设你需要维护一个列表,这 ...

  8. 【Android】读取sdcard卡上的全部图片而且显示,读取的过程有进度条显示

    尽管以下的app还没有做到快图浏览.ES文件浏览器的水平,遇到大sdcard还是会存在读取过久.内存溢出等问题,可是基本思想是这种. 例如以下图.在sdcard卡上有4张图片, 打开app,则会吧sd ...

  9. OCP读书笔记(2) - 配置恢复

    RMAN的命令类型 1. sqlplus命令 [oracle@oracle admin]$ export ORACLE_SID=orcl [oracle@oracle admin]$ rman tar ...

  10. IE 加速插件之 Google Chrome Frame

    前言 IE 8 及以下版本的速度较慢. 特别是前端的js 和 css 内容较多时尤为突出. 就笔者的开发经验来说GWT, Ext JS, raphael , draw2d 等开发的系统在IE下使用是相 ...