Mysql操作方法类
程序员文章站
2022-04-30 23:41:08
帮助类: using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using MySq... ......
帮助类: 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 = 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;database=ce"); private void button1_click(object 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;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
上一篇: js权威指南学习笔记(二)表达式与运算符
下一篇: redis集群宕机