C # manipulating MySQL's tool class Mysqlhelper

Source: Internet
Author: User
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 (); The 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 Mysqlcomm                      and (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 (); }          }      }
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.