帮助类:

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. Nim && Grundy (基础博弈游戏 )

    通常的Nim游戏的定义是这样的:有若干堆石子,每堆石子的数量都是有限的,合法的移动是“选择一堆石子并拿走若干颗(不能不拿)”,如果轮到某个人时所有的石子堆都已经被拿空了,则判负(因为他此刻没有任何合法 ...

  2. 一些有关PyCharm使用总结

    目前在这里,你能看见 license server Python版本配置 添加另外版本的Python 设置字体大小 关于编码 关于模版 安装好之后,第一个问题就是 license server 问题, ...

  3. 导入别的类中的bean

    @Configuration class CommonContext { @Bean public MyBolt myBolt() { return new MyBolt(); } } ... @Co ...

  4. 用servlet获取IP等信息

    Locale languageType=request.getLocale();//获取用户语言 String localIp=request.getLocalAddr();//获取本地ip int  ...

  5. powerdesigner添加唯一约束

    假设我们有一个user表,字段为ID和NAME,现在ID作为逻辑主键,自增,想将NAME添加唯一约束,话不多说直接上图: # 添加一个key, 名字随便取,我取为key_u # 双击添加的key的第一 ...

  6. Sqoop概述

    sqoop Sqoop 是传统数据库与 Hadoop 之间数据同步的工具,它是 Hadoop 发展到一定程度的必然产物,它主要解决的是传统数据库和Hadoop之间数据的迁移问题.这节课我们将详细介绍 ...

  7. PHP拾贝

    $_SERVER['DOCUMENT_ROOT']指向了web服务器文档树的根.(E:/wamp/www/) ********************************************* ...

  8. pat1034. Head of a Gang (30)

    1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue One wa ...

  9. MongoDB Linux 安装配置 后台运行

    介绍安装的文档很多,可以参考这篇: http://www.mkyong.com/mongodb/how-to-install-mongodb-on-mac-os-x/ 安装完后你可能会碰到的2个问题. ...

  10. client的使用

    document.documentElement.clientHeight = 464 // 指窗口的可见高度的大小 document.body.clientHeight = 1577 // 指窗口的 ...