zoukankan      html  css  js  c++  java
  • MySql_DBHelper

    public class DBHelper
        {
            //System.Data.SqlClient
            //Dapper
            //Newtonsoft.Json
    
            /// <summary>
            /// 查询List集合
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static List<T> GetList<T>(string sql)
            {
                try
                {
                    using (MySqlConnection conn=new MySqlConnection(AppsettingsHelper.AppConfigOwn.SqlConnString))
                    {
                        return conn.Query<T>(sql).ToList();
                    }
                }
                catch(Exception)
                {
                    throw;
                }
            }
    
            /// <summary>
            /// 增删改
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static int Exec(string sql)
            {          
                try
                {
                    using (MySqlConnection conn=new MySqlConnection(AppsettingsHelper.AppConfigOwn.SqlConnString))
                    {
                        return conn.Execute(sql);
                    }
                }
                catch(Exception)
                {
                    throw;
                }
            }
    
            /// <summary>
            /// 二级联动
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public int GetSaler(string sql)
            {
                try
                {
                    using (MySqlConnection conn=new MySqlConnection(AppsettingsHelper.AppConfigOwn.SqlConnString))
                    {
                        return conn.ExecuteScalar<int>(sql);
                    }
                }
                catch(Exception)
                {
                    throw;
                }
            }
    
            /// <summary>
            /// 使用事务执行sql
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static int ExecTransaction(string sql)
            {
                try
                {
                    using (MySqlConnection conn = new MySqlConnection(AppsettingsHelper.AppConfigOwn.SqlConnString))
                    {
                        IDbTransaction transaction = conn.BeginTransaction();
                        int a= conn.Query<int>(sql,transaction).AsList()[0];
                        transaction.Commit();
                        return a;
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
    
            /// <summary>
            /// 返回首行首列
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static int ExexCount(string sql)
            {
                try
                {
                    using (MySqlConnection conn = new MySqlConnection(AppsettingsHelper.AppConfigOwn.SqlConnString))
                    {
                        return conn.ExecuteScalar<int>(sql);
                    }
                }
                catch (Exception)
                {
                    throw;
                }
            }
        }
  • 相关阅读:
    周报说明8-4
    周报7-28
    日志-maven-c监控 周报7-21
    证明创建runnable实例和普通类时间一样长, 其实吧
    UE4开始之路
    js简单框架设计
    json中key为数字会自动排序问题
    u3动画
    工厂模式
    单例模式
  • 原文地址:https://www.cnblogs.com/lyboke/p/14837783.html
Copyright © 2011-2022 走看看