.net MVC dapper 帮助类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using Dapper;

namespace BaseDao
{
    /// 
    /// Dapper框架 增删改查事务帮助类
    /// 
    /// 
    public static class DbHelper
    {
        private static readonly string connStr = ConfigurationManager.ConnectionStrings["sqlConnStr"].ConnectionString;
        #region 增删改
        /// 
        /// 增删改
        /// 
        /// sql语句
        /// 实体放
        /// 
        public static int ExecuteNonQuery(string sqlQuery, object param)
        {
            using (SqlConnection db = new SqlConnection(connStr))
            {
                db.Open();
                return db.Execute(sqlQuery, param);
            }
        }
        #endregion
        #region 根据条件查询返回单个实体
        /// 
        /// 根据条件查询返回单个实体
        /// 
        /// 
        /// 
        public static T GetEntityBy(string sqlQuery, object param)
        {
            using (SqlConnection db = new SqlConnection(connStr))
            {
                db.Open();
                return db.QuerySingleOrDefault(sqlQuery, param);
            }
        }
        #endregion
        #region 查询一张表
        /// 
        /// 查询一张表
        /// 
        /// 
        /// 
        public static List GetListBy(string sqlQuery, object param)
        {
            using (SqlConnection db = new SqlConnection(connStr))
            {
                db.Open();
                return db.Query(sqlQuery, param).ToList();
            }
        }
        #endregion
        #region 事务1 全SQL
        /// 
        /// 事务1 - 全SQL
        /// 
        /// 多条SQL
        /// param
        /// 
        public static int ExecuteTransaction(string[] sqlarr)
        {
            using (SqlConnection con = new SqlConnection(connStr))
            {
                con.Open();
                using (var transaction = con.BeginTransaction())
                {
                    try
                    {
                        int result = 0;
                        foreach (var sql in sqlarr)
                        {
                            result += con.Execute(sql, null, transaction);
                        }

                        transaction.Commit();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }
        #endregion
        #region 事务2 - 声明参数
        /// 
        /// 事务2 - 声明参数
        ///demo:
        ///dic.Add("Insert into Users values (@UserName, @Email, @Address)",
        ///        new { UserName = "jack", Email = "[email protected]", Address = "上海" });
        /// 
        /// 多条SQL
        /// param
        /// 
        public static int ExecuteTransaction(Dictionary dic)
        {
            using (SqlConnection con = new SqlConnection(connStr))
            {
                con.Open();
                using (var transaction = con.BeginTransaction())
                {
                    try
                    {
                        int result = 0;
                        foreach (var sql in dic)
                        {
                            result += con.Execute(sql.Key, sql.Value, transaction);
                        }
                        transaction.Commit();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }
        #endregion
        #region 批量删除
        /// 
        /// 批量删除
        /// 
        /// 
        /// 
        /// 
        public static int DelList(string sql, List list)
        {
            using (SqlConnection db = new SqlConnection(connStr))
            {
                db.Open();
                using (var tran = db.BeginTransaction())
                {
                    try
                    {
                        int result = 0;
                        result += db.Execute(sql, list, tran);
                        tran.Commit();
                        return result;
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        return 0;
                    }
                }
            }
        } 
        #endregion
    }
}

 

你可能感兴趣的:(.net,小白轻松学c#.net)