mysql新建data的方法_分享一个Mysql操作方法类(MySql数据库连接、执行MySqlCommand命令、创建MySqlDataReader对象)...

帮助类:usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingMySql.Data.MySqlClient;namespaceTestMYSQL

{public classMySqlHelper

{string M_str_sqlcon = string.Empty;privateMySqlHelper()

{

}public MySqlHelper(stringstr_sqlcon)

{

M_str_sqlcon=str_sqlcon;

}#region 建立MySql数据库连接

///

///建立数据库连接.///

/// 返回MySqlConnection对象

privateMySqlConnection getmysqlcon()

{//string M_str_sqlcon = "server=localhost;user id=root;password=root;database=abc";//根据自己的设置

MySqlConnection myCon = newMySqlConnection(M_str_sqlcon);returnmyCon;

}#endregion

#region 执行MySqlCommand命令

///

///执行MySqlCommand///

/// SQL语句

public int getmysqlcom(stringM_str_sqlstr)

{int rel = 0;

MySqlConnection mysqlcon=null;

MySqlCommand mysqlcom=null;try{

mysqlcon= this.getmysqlcon();

mysqlcon.Open();

mysqlcom= newMySqlCommand(M_str_sqlstr, mysqlcon);

rel=mysqlcom.ExecuteNonQuery();returnrel;

}catch(Exception ex)

{throwex;

}finally{if (mysqlcom != null)

{

mysqlcom.Dispose();

}if (mysqlcon != null)

{

mysqlcon.Close();

mysqlcon.Dispose();

}

}

}#endregion

#region 创建MySqlDataReader对象

///

///创建一个MySqlDataReader对象///

/// SQL语句

/// 返回MySqlDataReader对象

public MySqlDataReader getmysqlread(stringM_str_sqlstr)

{

MySqlConnection mysqlcon= null;

MySqlCommand mysqlcom= null;try{

mysqlcon= this.getmysqlcon();

mysqlcom= newMySqlCommand(M_str_sqlstr, mysqlcon);

mysqlcon.Open();

MySqlDataReader mysqlread=mysqlcom.ExecuteReader(CommandBehavior.CloseConnection);returnmysqlread;

}catch(Exception ex)

{throwex;

}finally{if (mysqlcom != null)

{

mysqlcom.Dispose();

}if (mysqlcon != null)

{

mysqlcon.Close();

mysqlcon.Dispose();

}

}

}#endregion}

}

后台:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceSQLToMysql_Move

{public partial classForm1 : Form

{publicForm1()

{

InitializeComponent();

}

TestMYSQL.MySqlHelper mysql= new TestMYSQL.MySqlHelper("server=127.0.0.1;user id=root;password=123456;database=ce");private void button1_Click(objectsender, 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)

{throwex;

}//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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值