首页 技术 正文
技术 2022年11月14日
0 收藏 385 点赞 4,783 浏览 3006 个字
帮助类: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
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294