数据库
首页 > 数据库> > MySql_DBHelper

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

 

标签:int,MySql,DBHelper,sql,catch,MySqlConnection,public,conn
来源: https://www.cnblogs.com/lyboke/p/14837783.html