• 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
  • 相关阅读:
    js post 异步请求
    Android 实现文件上传功能(upload)
    js 金额文本框实现代码
    纯CSS画的基本图形(矩形、圆形、三角形、多边形、爱心、八卦等)
    NBearV2视频教学系列总索引,欢迎多提意见和建议[09/21更新至IoC篇]
    1.2 实体实例化及使用自定义实体[发布时间:9/6]
    NBear视频 4.1 基于NBear.IoC的企业级系统构架[发布时间:9/21]
    2.1 基于NBear.Data的实体持久化[发布时间:9/10]
    NBear案例源码 简易AJAX留言板 [Updated 10/31 Powered by NBear V3.0.0 preview]
    全面解析ASP.NET2.0下的URL重写
  • 原文地址:https://www.cnblogs.com/equation/p/10625391.html
Copyright © 2020-2023  润新知