C#操作MySQL的工具類MySqlHelper

來源:互聯網
上載者: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();          }                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();              }          }      }
  • 相關文章

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.