帮助类: 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;data =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 = 0; 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;data =ce"); private void button1_Click( sender, EventArgs e) { int rel = 0; try { DataSet dataset = Common.DbHelperSQL.Query("select * from dbo.Num"); DataTable dt = dataset.Tables[0]; dataGridView1.DataSource = dt; for (int i = 0; i < dt.Rows.Count ; i++) { label1.Text = dt.Rows[i][0].ToString(); label2.Text = dt.Rows[i][1].ToString(); rel = mysql.getmysqlcom("INSERT INTO `ce`.`notice` (`Content`, `Start_date`, `End_date`) VALUES ('" + dt.Rows[i][1].ToString() + "', '" + dt.Rows[i][0].ToString() + "', '2');"); } MessageBox.Show((rel > 0) ? "成功" : "失败"); } catch (Exception ex) { throw ex; } //TestMYSQL.MySqlHelper mysql = new TestMYSQL.MySqlHelper("server=127.0.0.1;user id=root;password=123456;data =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
继续阅读与本文标签相同的文章
上一篇 :
【数据分析】贝叶斯原理以及简单案例说明
-
开发者云阿拉丁神灯计划参与规则
2026-05-18栏目: 教程
-
什么?还没听说过Prometheus,或许你需要了解这些知识点
2026-05-18栏目: 教程
-
Knative 实战:三步走!基于 Knative Serverless 技术实现一个短网址服务
2026-05-18栏目: 教程
-
开篇 | 揭秘 Flink 1.9 新架构,Blink Planner 你会用了吗? | 9月20号栖夜读
2026-05-18栏目: 教程
-
精彩回放|《安全说道》节目合集
2026-05-18栏目: 教程
