本文執行個體講述了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#程式設計有所協助。