.net中连接MYSQL数据库及常用操作

本文介绍如何在.NET环境中建立与MySQL数据库的连接,并提供了执行常见数据库操作的工具类示例,同时附带了一个用于记录日志的.NET类。
摘要由CSDN通过智能技术生成

需要有一个工具类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Collections;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
using System.Configuration;
using System.IO;



public abstract class MySqlHelper
{
    //Get the database connectionstring, which are static variables and readonly, all project documents can be used directly, but can not modify it 
    //the database connectionString 
    //public static readonly string connectionStringManager = ConfigurationManager.ConnectionStrings["MYSQLConnectionString"].ConnectionString;
    public static string ConnectionStringManager
    {
        get { return connectionStringManager; }
    }



    //This connectionString for the local test
    public static readonly string connectionStringManager = "Server=xxxxxxx;Database=91bx;Uid=xxxx;Pwd=xxxxx;pooling=false;charset=utf8"; //System.Configuration.ConfigurationManager.AppSettings["MySQLConnString"];
    //ConfigurationManager.ConnectionStrings["MySQLConnString"].ConnectionString;

    //hashtable to store the parameter information, the hash table can store any type of argument 
    //Here the hashtable is static types of static variables, since it is static, that is a definition of global use.
    //All parameters are using this hash table, how to ensure that others in the change does not affect their time to read it
    //Before ,the method can use the lock method to lock the table, does not allow others to modify.when it has readed then  unlocked table.
    //Now .NET provides a HashTable's Synchronized methods to achieve the same function, no need to manually lock, completed directly by the system framework 
    private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());

    /// <summary>
    /// Execute a SqlCommand command that does not return value, by appointed and specified connectionstring 
    /// The parameter list using parameters that in array forms
    /// </summary>
    /// <remarks>
    /// Usage example: 
    /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure,
    /// "PublishOrders", new MySqlParameter("@prodid", 24));
    /// </remarks>
    /// <param name="connectionString">a valid database connectionstring</param>
    /// <param name="cmdType">MySqlCommand command type (stored procedures, T-SQL statement, and so on.) </param>
    /// <param name="cmdText">stored procedure name or T-SQL statement</param>
    /// <param name="commandParameters">MySqlCommand to provide an array of parameters used in the list</param>
    /// <returns>Returns a value that means number of rows affected/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>
    /// Execute a SqlCommand command that does not return value, by appointed and specified connectionstring 
    /// The parameter list using parameters that in array forms
    /// </summary>
    /// <remarks>
    /// Usage example: 
    /// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure,
    /// "PublishOrders", new MySqlParameter("@prodid", 24));
    /// </remarks>
    /// <param name="cmdType">MySqlCommand command type (stored procedures, T-SQL statement, and so on.) </param>
    /// <param name="connectionString">a valid database connectionstring</param>
    /// <param name="cmdText">stored procedure name or T-SQL statement</param>
    /// <param name="commandParameters">MySqlCommand to provide an array of parameters used in the list</param>
    /// <returns>Returns true or false </returns>
    public static bool ExecuteNonQuery(CommandType cmdType, string connectionString, string cmdText, params MySqlParameter[] commandParameters)
    {
        MySqlCommand cmd = new MySqlCommand();

        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
            try
            {
                int val = cmd.ExecuteNonQuery();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                cmd.Parameters.Clear();
            }
        }
    }
    /// <summary>
    /// Execute a SqlCommand command that does not return value, by appointed and specified connectionstring 
    /// Array of form parameters using the parameter list 
    /// </summary>
    /// <param name="conn">connection</param>
    /// <param name="cmdType">MySqlCommand command type (stored procedures, T-SQL statement, and so on.)</param>
    /// <param name="cmdText">stored procedure name or T-SQL statement</param>
    /// <param name="commandParameters">MySqlCommand to provide an array of parameters used in the list</param>
    /// <returns>Returns a value that means number of rows affected</returns>
    public static int ExecuteNonQuery(MySqlConnection conn, CommandType cmdType, string cmdText, params MySqlParameter[] commandParameters)
    {
        MySqlCommand cmd = new MySqlCommand();
        PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
        int val = cmd.ExecuteNonQuery();
        cmd.Parameters.Clear();
        return val;
    }

    /// <summary>
    /// Execute a SqlCommand command that does not return value, by appointed and specified connectionstring 
    /// Array of form parameters using the parameter list 
    /// </summary>
    /// <param name="conn">sql Connection that has transaction</param>
    /// <param name="cmdType">SqlCommand command type (stored procedures, T-SQL statement, and so on.)</param>
    /// <param name="cmdText">stored procedure name or T-SQL statement</param>
    /// <param name="commandParameters">MySqlCommand to provide an array of parameters used in the list</param>
    /// <returns>Returns a value that 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值