帮助类:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient; namespace TestMYSQL
{
public class MySqlHelper
{
string M_str_sqlcon = string.Empty; private MySqlHelper()
{
}
public MySqlHelper(string str_sqlcon)
{
M_str_sqlcon = str_sqlcon;
}
#region 建立MySql数据库连接
/// <summary>
/// 建立数据库连接.
/// </summary>
/// <returns>返回MySqlConnection对象</returns>
private MySqlConnection getmysqlcon()
{
//string M_str_sqlcon = "server=localhost;user id=root;password=root;database=abc"; //根据自己的设置
MySqlConnection myCon = new MySqlConnection(M_str_sqlcon);
return myCon;
}
#endregion #region 执行MySqlCommand命令
/// <summary>
/// 执行MySqlCommand
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
public int getmysqlcom(string M_str_sqlstr)
{
int rel = ;
MySqlConnection mysqlcon=null;
MySqlCommand mysqlcom=null;
try
{
mysqlcon = this.getmysqlcon();
mysqlcon.Open();
mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon);
rel = mysqlcom.ExecuteNonQuery();
return rel;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mysqlcom != null)
{
mysqlcom.Dispose();
}
if (mysqlcon != null)
{
mysqlcon.Close();
mysqlcon.Dispose();
}
}
}
#endregion #region 创建MySqlDataReader对象
/// <summary>
/// 创建一个MySqlDataReader对象
/// </summary>
/// <param name="M_str_sqlstr">SQL语句</param>
/// <returns>返回MySqlDataReader对象</returns>
public MySqlDataReader getmysqlread(string M_str_sqlstr)
{
MySqlConnection mysqlcon = null;
MySqlCommand mysqlcom = null;
try
{
mysqlcon = this.getmysqlcon();
mysqlcom = new MySqlCommand(M_str_sqlstr, mysqlcon);
mysqlcon.Open();
MySqlDataReader mysqlread = mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);
return mysqlread;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (mysqlcom != null)
{
mysqlcom.Dispose();
}
if (mysqlcon != null)
{
mysqlcon.Close();
mysqlcon.Dispose();
}
}
}
#endregion }
} 后台: using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SQLToMysql_Move
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
TestMYSQL.MySqlHelper mysql = new TestMYSQL.MySqlHelper("server=127.0.0.1;user id=root;password=123456;database=ce");
private void button1_Click(object sender, EventArgs e)
{
int rel = ;
try
{ DataSet dataset = Common.DbHelperSQL.Query("select * from dbo.Num");
DataTable dt = dataset.Tables[];
dataGridView1.DataSource = dt;
for (int i = ; i < dt.Rows.Count ; i++)
{
label1.Text = dt.Rows[i][].ToString();
label2.Text = dt.Rows[i][].ToString();
rel = mysql.getmysqlcom("INSERT INTO `ce`.`notice` (`Content`, `Start_date`, `End_date`) VALUES ('" + dt.Rows[i][].ToString() + "', '" + dt.Rows[i][].ToString() + "', '2');");
}
MessageBox.Show((rel > ) ? "成功" : "失败");
}
catch (Exception ex)
{
throw ex;
}
//TestMYSQL.MySqlHelper mysql = new TestMYSQL.MySqlHelper("server=127.0.0.1;user id=root;password=123456;database=ce");
//string sql = "INSERT INTO `ce`.`notice` (`Id`, `Content`, `Start_date`, `End_date`) VALUES ('2', '2', '2', '2');";
//try
//{
// int rel = mysql.getmysqlcom(sql);
// MessageBox.Show((rel > 0) ? "成功" : "失败");
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}
}
}
}
相关DLL:
https://i.cnblogs.com/Files.aspx

Mysql操作方法类的更多相关文章

  1. ecshop的Mysql操作类

    摘要,这是直接摘抄的ecshop的mysql操作类:不过他这里的缓存是用的文件缓存,我们如果想直接使用,可以替换成memcache的或者redis的! <?php /** * ECSHOP MY ...

  2. 深入理解php的MySQL连接类

    php的MySQL连接类.  后面几个show_databases和show_tables....等方法都用了一堆echo,好像一直不喜欢在类的方法里直接用输出语句,不过这也只是列举数据库和表名,构造 ...

  3. php+mysql分页类的入门实例

    php+mysql分页类的简单代码 时间:2016-02-25 06:16:26来源:网络 导读:php+mysql分页类的简单代码,二个php分页类代码,对商品进行分页展示,当前页面数,每个页面展示 ...

  4. C#操作MySQL的类

    C#操作MySQL的类 public class MySqlService { private static log4net.ILog logger = log4net.LogManager.GetL ...

  5. PHP:自己写的mysql操作类

    a{ font-weight: bold; display: block; text-align: center; color: #5887bf; font-size: 22px; } .conten ...

  6. 着重基础之—MySql Blob类型和Text类型

    着重基础之—MySql Blob类型和Text类型 在经历了几个Java项目后,遇到了一些问题,在解决问题中体会到基础需要不断的回顾与巩固. 最近做的项目中,提供给接口调用方数据同步接口,传输的数据格 ...

  7. mysql连接类与ORM的封装

    ORM: - ORM什么是? 类名 ---> 数据库表 对象 ---> 记录 对象.属性 ---> 字段 - ORM的优缺点: 优点: 可跨平台,可以通过对象.属性取值,对象.方法, ...

  8. php--->单例模式封装mysql操作类

    php 单例模式封装mysql操作类 单例模式的必要条件(三私一公) 私有的成员属性--防止类外引入这个存放对象的属性 私有的构造方法--为了防止在类外使用new关键字实例化对象 私有的克隆方法--为 ...

  9. C# MySql 操作类

    /* MySql 类 */ using System; using System.Collections.Generic; using System.Linq; using System.Text; ...

随机推荐

  1. PHPExcel探索之旅---阶段二 设置表格样式

    1.设置表格的默认样式为水平居中.垂直居中 getDefaultStyle()函数用来设置默认样式 由活动sheet对象来调用,setVertical()函数和setHorizontal()函数分别用 ...

  2. 牛客假日团队赛1 J.分组

    链接: https://ac.nowcoder.com/acm/contest/918/J 题意: 在Farmer John最喜欢的节日里,他想要给他的朋友们赠送一些礼物.由于他并不擅长包装礼物,他想 ...

  3. ProtobufUtils

    import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; import com. ...

  4. bootstrap栅格系统的实现

    bootstrap提供了一个非常实用的栅格系统,可以实现响应式的网格布局,原理其实很简单,利用了float.百分比的宽度和@media的配合实现响应式,bootstrap默认把一行分为了12列,提供了 ...

  5. fiddle

    web开发中Chrome.IE.firefox等浏览器都自带提供了插件帮助开发者跟踪http数据,在手机客户端怎么实现http数据抓包呢?Fiddler可以实现真机调试抓包.Fiddler支持Any ...

  6. HDU 5775 L - Bubble Sort 树状数组

    给定一段冒泡排序的代码,要求输出每个数字能到达的最右边的位置和最左边的位置的差 因为那段冒泡排序的代码是每次选取一个最小的数,放在左边的,所以,每个数最多能到达右边的位置应该是起始位置i+右边有多少个 ...

  7. python3+Appium自动化08-数据配置yaml

    yaml简介 yaml是一种简洁的非标记语言.yaml以数据为中心,使用空白,缩进,分行组织数据,从而使得表示更加简洁易读 由于实现简单,解析成本低,yaml特别适合在脚本语言中使用.现有的语言实现: ...

  8. python学习二(文件与异常)

    Python中使用open BIF与文件交互,与for语句结合使用,一次读取一行 读取文件sketch.txt,文件内容如下: Man: Ah! (taking out his wallet and ...

  9. ASP.NET相关

    1.委托:把一个方法当作参数传到另一个方法中 扩展方法:1.静态类 2.静态方法 3.this关键字 using System; using System.Collections.Generic; u ...

  10. 来自于51CTO的经典学习资料汇总

    移动开发类: 1.2012Android开发热门资料(110个)       http://bbs.51cto.com/thread-934023-1.html 2.[绝对给力]Android开发免豆 ...