程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#完成操作MySql數據層類MysqlHelper實例

C#完成操作MySql數據層類MysqlHelper實例

編輯:C#入門知識

C#完成操作MySql數據層類MysqlHelper實例。本站提示廣大學習愛好者:(C#完成操作MySql數據層類MysqlHelper實例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成操作MySql數據層類MysqlHelper實例正文


本文實例講述了C#完成操作MySql數據層類MysqlHelper。分享給年夜家供年夜家參考。詳細以下:

using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using MySql.Data;
using MySql.Data.MySqlClient;
namespace VideoWorks.ITmanage.DAL
{
 public abstract class MySqlHelper
 {
  //數據庫銜接字符串
  public static string Conn = "Database='device_manage';Data Source='localhost';User Id='root';Password='123456';charset='utf8';pooling=true;Allow Zero Datetime=True";
  /// <summary>
  /// 給定銜接的數據庫用假定參數履行一個sql敕令(不前往數據集)
  /// </summary>
  /// <param name="connectionString">一個有用的銜接字符串</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns>履行敕令所影響的行數</returns>
  public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   MySqlCommand cmd = new MySqlCommand();
   using (MySqlConnection conn = new MySqlConnection(connectionString))
   {
    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
    int val = cmd.ExecuteNonQuery();
    cmd.Parameters.Clear();
    return val;
   }
  }
  /// <summary>
  /// 用現有的數據庫銜接履行一個sql敕令(不前往數據集)
  /// </summary>
  /// <param name="connection">一個現有的數據庫銜接</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns>履行敕令所影響的行數</returns>
  public static int ExecuteNonQuery(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   MySqlCommand cmd = new MySqlCommand();
   PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
   int val = cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   return val;
  }
  /// <summary>
  ///應用現有的SQL事務履行一個sql敕令(不前往數據集)
  /// </summary>
  /// <remarks>
  ///舉例:
  /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
  /// </remarks>
  /// <param name="trans">一個現有的事務</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns>履行敕令所影響的行數</returns>
  public static int ExecuteNonQuery(MySqlTransaction trans, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   MySqlCommand cmd = new MySqlCommand();
   PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
   int val = cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   return val;
  }
  /// <summary>
  /// 用履行的數據庫銜接履行一個前往數據集的sql敕令
  /// </summary>
  /// <remarks>
  /// 舉例:
  /// MySqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
  /// </remarks>
  /// <param name="connectionString">一個有用的銜接字符串</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns>包括成果的讀取器</returns>
  public static MySqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   //創立一個MySqlCommand對象
   MySqlCommand cmd = new MySqlCommand();
   //創立一個MySqlConnection對象
   MySqlConnection conn = new MySqlConnection(connectionString);
   //在這裡我們用一個try/catch構造履行sql文本敕令/存儲進程,由於假如這個辦法發生一個異常我們要封閉銜接,由於沒有讀取器存在,
   //是以commandBehaviour.CloseConnection 就不會履行
   try
   {
    //挪用 PrepareCommand 辦法,對 MySqlCommand 對象設置參數
    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
    //挪用 MySqlCommand 的 ExecuteReader 辦法
    MySqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    //消除參數
    cmd.Parameters.Clear();
    return reader;
   }
   catch
   {
    //封閉銜接,拋出異常
    conn.Close();
    throw;
   }
  }
  /// <summary>
  /// 前往DataSet
  /// </summary>
  /// <param name="connectionString">一個有用的銜接字符串</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns></returns>
  public static DataSet GetDataSet(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   //創立一個MySqlCommand對象
   MySqlCommand cmd = new MySqlCommand();
   //創立一個MySqlConnection對象
   MySqlConnection conn = new MySqlConnection(connectionString);
   //在這裡我們用一個try/catch構造履行sql文本敕令/存儲進程,
   //由於假如這個辦法發生一個異常我們要封閉銜接,由於沒有讀取器存在,
   try
   {
    //挪用 PrepareCommand 辦法,對 MySqlCommand 對象設置參數
    PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
    //挪用 MySqlCommand 的 ExecuteReader 辦法
    MySqlDataAdapter adapter = new MySqlDataAdapter();
    adapter.SelectCommand = cmd;
    DataSet ds = new DataSet();
    adapter.Fill(ds);
    //消除參數
    cmd.Parameters.Clear();
    conn.Close();
    return ds;
   }
   catch (Exception e)
   {
    throw e;
   }
  }
  /// <summary>
  /// 用指定的數據庫銜接字符串履行一個敕令並前往一個數據集的第一列
  /// </summary>
  /// <remarks>
  ///例如:
  /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
  /// </remarks>
  ///<param name="connectionString">一個有用的銜接字符串</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns>用 Convert.To{Type}把類型轉換為想要的 </returns>
  public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   MySqlCommand cmd = new MySqlCommand();
   using (MySqlConnection connection = new MySqlConnection(connectionString))
   {
    PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
    object val = cmd.ExecuteScalar();
    cmd.Parameters.Clear();
    return val;
   }
  }
  /// <summary>
  /// 用指定的數據庫銜接履行一個敕令並前往一個數據集的第一列
  /// </summary>
  /// <remarks>
  /// 例如:
  /// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new MySqlParameter("@prodid", 24));
  /// </remarks>
  /// <param name="connection">一個存在的數據庫銜接</param>
  /// <param name="cmdType">敕令類型(存儲進程, 文本, 等等)</param>
  /// <param name="cmdText">存儲進程稱號或許sql敕令語句</param>
  /// <param name="commandParameters">履行敕令所用參數的聚集</param>
  /// <returns>用 Convert.To{Type}把類型轉換為想要的 </returns>
  public static object ExecuteScalar(MySqlConnection connection, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
  {
   MySqlCommand cmd = new MySqlCommand();
   PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
   object val = cmd.ExecuteScalar();
   cmd.Parameters.Clear();
   return val;
  }
  /// <summary>
  /// 預備履行一個敕令
  /// </summary>
  /// <param name="cmd">sql敕令</param>
  /// <param name="conn">OleDb銜接</param>
  /// <param name="trans">OleDb事務</param>
  /// <param name="cmdType">敕令類型例如 存儲進程或許文本</param>
  /// <param name="cmdText">敕令文本,例如:Select * from Products</param>
  /// <param name="cmdParms">履行敕令的參數</param>
  private static void PrepareCommand(MySqlCommand cmd, MySqlConnection conn, MySqlTransaction trans, CommandType cmdType, string cmdText, MySqlParameter[] cmdParms)
  {
   if (conn.State != ConnectionState.Open)
    conn.Open();
   cmd.Connection = conn;
   cmd.CommandText = cmdText;
   if (trans != null)
    cmd.Transaction = trans;
   cmd.CommandType = cmdType;
   if (cmdParms != null)
   {
    foreach (MySqlParameter parm in cmdParms)
     cmd.Parameters.Add(parm);
   }
  }
 }
}

願望本文所述對年夜家的C#法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved