C# vs MySql

 

 

MySqlHelper類sql

    /// <summary>
    ///MySql操做類
    /// </summary>
    public abstract class MySqlHelper
    {
        //數據庫鏈接字符串
        public static string Conn = "Database='RDBaoApp';Data Source='192.168.3.199';User Id='root';Password='123456';charset='utf8';pooling=true;allow zero datetime=true";

        // 用於緩存參數的HASH表
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

        /// <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="cacheKey">添加到緩存的變量</param>
        /// <param name="commandParameters">一個將要添加到緩存的sql參數集合</param>
        public static void CacheParameters(string cacheKey, params MySqlParameter[] commandParameters)
        {
            parmCache[cacheKey] = commandParameters;
        }

        /// <summary>
        /// 找回緩存參數集合
        /// </summary>
        /// <param name="cacheKey">用於找回參數的關鍵字</param>
        /// <returns>緩存的參數集合</returns>
        public static MySqlParameter[] GetCachedParameters(string cacheKey)
        {
            MySqlParameter[] cachedParms = (MySqlParameter[])parmCache[cacheKey];

            if (cachedParms == null)
                return null;

            MySqlParameter[] clonedParms = new MySqlParameter[cachedParms.Length];

            for (int i = 0, j = cachedParms.Length; i < j; i++)
                clonedParms[i] = (MySqlParameter)((ICloneable)cachedParms[i]).Clone();

            return clonedParms;
        }

        /// <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);
            }
        }

    }

調用方式數據庫

 DataTable dt = MySqlHelper.GetDataSet(MySqlHelper.Conn, CommandType.Text, "select * from ContractEmail", null).Tables[0];
相關文章
相關標籤/搜索