MySql教程

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

 

这篇关于MySql_DBHelper的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!