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;
                }
            }
        }
  • 相关阅读:
    5,MongoDB 之 "$" 的奇妙用法
    4,MongoDB 之 $关键字 及 $修改器 $set $inc $push $pull $pop MongoDB
    3,MongoDB之数据类型
    2,MongoDB之增删改查及pymongo的使用
    1,MongoDB简介和安装
    19,Ubuntu安装之python开发
    18,Shell编程实战
    17,saltstack高效运维
    16.2,docker网络
    如何使用 window api 转换字符集?
  • 原文地址:https://www.cnblogs.com/lyboke/p/14837783.html
Copyright © 2011-2022 走看看