C#操作MySQL的类

Posted equation

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#操作MySQL的类相关的知识,希望对你有一定的参考价值。

C#操作mysql的类

 

public class MySqlService
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MySqlService));

        //server=localhost;user id=root;password=root;persist security info=True;database=test
        //Data Source=127.0.0.1;port=3306;Initial Catalog=tsyw;user id=root;password=q2ii3sfc;Charset=gbk
        private string conn_str = "server=localhost;user id=root;password=q2ii3sfc;persist security info=True;database=win008";

        public MySqlService()
        {

        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="conn_str"></param>
        public MySqlService(string conn_str)
        {
            this.conn_str = conn_str;
        }

        /// <summary>
        /// 执行sql语句返回List-Hashtable数据集
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public List<Hashtable> Select(string sql)
        {
            List<Hashtable> lst = new List<Hashtable>();
            using (MySqlConnection connection = new MySqlConnection(this.conn_str))
            {
                try
                {
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    MySqlDataReader mdr = cmd.ExecuteReader();
                    int columnCount = mdr.FieldCount;
                    string _key = string.Empty;
                    while (mdr.Read())
                    {
                        Hashtable ht = new Hashtable();
                        for(int i = 0; i < columnCount; i++)
                        {
                            _key = mdr.GetName(i);
                            ht[_key] = mdr[i];
                        }

                        lst.Add(ht);
                    }
                    mdr.Close();
                    connection.Close();
                }
                catch (Exception ex)
                {
                    logger.Warn("执行操作语句异常, SQL:" + sql, ex);
                }
            }
            return lst;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public System.Data.DataSet Query(string sql)
        {
            return null;
        }

        /// <summary>
        /// 执行操作语句 delete/insert/update
        /// <para>返回影响行数</para>
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public int Execute(string sql)
        {
            int rest = 0;
            using (MySqlConnection connection = new MySqlConnection(this.conn_str))
            {
                try
                {
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    rest = cmd.ExecuteNonQuery();
                    rest = (int)(cmd.LastInsertedId>0?cmd.LastInsertedId:rest);

                    connection.Close();
                }
                catch (Exception ex)
                {
                    logger.Warn("执行操作语句异常, SQL:" + sql, ex);
                }
            }

            return rest;
        }

        public Hashtable FetchRow(string sql)
        {
            Hashtable ht = new Hashtable();
            using (MySqlConnection connection = new MySqlConnection(this.conn_str))
            {
                try
                {
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    MySqlDataReader mdr = cmd.ExecuteReader();
                    int columnCount = mdr.FieldCount;
                    string _key = string.Empty;
                    while (mdr.Read())
                    {
                        for (int i = 0; i < columnCount; i++)
                        {
                            _key = mdr.GetName(i);
                            ht[_key] = mdr[i];
                        }
                        break;
                    }
                    mdr.Close();
                    connection.Close();
                }
                catch (Exception ex)
                {
                    logger.Warn("执行操作语句异常, SQL:" + sql, ex);
                }
            }
            return ht;
        }

        /// <summary>
        /// 获取语句第一行第一列数据
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public T FetchFirst<T>(string sql) {
            T t;
            using (MySqlConnection connection = new MySqlConnection(this.conn_str))
            {
                try
                {
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand(sql, connection);
                    t = (T)cmd.ExecuteScalar();
                    connection.Close();
                }
                catch (Exception ex)
                {
                    t = default(T);
                    logger.Warn("执行操作语句异常, SQL:" + sql, ex);
                }
            }
            return t;
        }

        /// <summary>
        /// 检查链接是否OK
        /// </summary>
        /// <returns></returns>
        public bool Ping()
        {
            bool rest = false;
            using (MySqlConnection connection = new MySqlConnection(this.conn_str))
            {
                try
                {
                    connection.Open();
                    MySqlCommand cmd = new MySqlCommand("select 1", connection);
                    cmd.ExecuteScalar();
                    connection.Close();
                    rest = true;
                }
                catch
                {
                    rest = false;
                }
            }

            return rest;
        }

    }

 

C#操作MySQL的类
[C#cāozuò MySQL de lèi]
技术图片
C# operation MySQL class

以上是关于C#操作MySQL的类的主要内容,如果未能解决你的问题,请参考以下文章

C#程序员经常用到的10个实用代码片段

C# - 使项目 MySQL 和 SQL Server 兼容

在 Visual Studio 中创建构造函数的代码片段或快捷方式

C# 最有用的(自定义)代码片段是啥? [关闭]

c#代码片段快速构建代码

此 Canon SDK C++ 代码片段的等效 C# 代码是啥?