您好,欢迎访问一九零五行业门户网

C#操作MySQL的工具类MySqlHelper

using system; using system.collections.generic; using system.linq; using system.text; using mysql.data.mysqlclient; using system.data; class mysqlhelper:idisposable { private mysqlconnection m_conn = null; private mysqltransaction m_trans = null; private bool m_tran_enabled = false; public mysqlhelper() { m_conn = new mysqlconnection(); m_conn.connectionstring = server=localhost;port=3301;uid=sa;pwd=000; m_conn.open(); } public void begintrans() { m_trans = m_conn.begintransaction(); m_tran_enabled = true; } public void commit() { if (m_trans != null && m_tran_enabled) { m_tran_enabled = false; m_trans.commit(); } } public void rollback() { if (m_trans != null && m_tran_enabled) { m_tran_enabled = false; m_trans.rollback(); } } public object querysome(string sql,int fieldindex) { using (mysqlcommand cmd = new mysqlcommand(sql, m_conn)) { using (mysqldatareader sr = cmd.executereader()) { if (sr.read()) { return sr.getvalue(fieldindex); } } } return null; } public delegate void fillvalues(mysqldatareader sr); public void querysomes(string sql, fillvalues fill) { using (mysqlcommand cmd = new mysqlcommand(sql, m_conn)) { using (mysqldatareader sr = cmd.executereader()) { fill(sr); } } } public datatable source(string sql) { datatable dt = null; mysqlcommand cmd = null; mysqldataadapter ad = null; try { lock (dt = new datatable()) { cmd = new mysqlcommand(sql, m_conn); ad = new mysqldataadapter((mysqlcommand)cmd); dt.clear(); ad.fill(dt); } } catch (exception e) { throw e; } return dt; } public void execproc(string proc, params mysqlparameter[] ps) { using (mysqlcommand cmd = new mysqlcommand(proc, m_conn)) { cmd.commandtype = system.data.commandtype.storedprocedure; foreach (mysqlparameter p in ps) { cmd.parameters.add(p); } cmd.executenonquery(); } } void idisposable.dispose() { m_conn.close(); m_conn.dispose(); if (m_trans != null) { m_trans.dispose(); } } }
其它类似信息

推荐信息