自己整理了一个 Dapper的Helper助手类

web.config 链接字符串配置:

  
    
  

DapperHelper.cs :

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

namespace DapperDAL
{
    public class DapperHelper
    {
        /// 
        /// 数据库连接字符串
        /// 
        private static readonly string connectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;

        /// 
        /// 查询列表
        /// 
        /// 查询的sql
        /// 替换参数
        /// 
        public static List Query(string sql, object param = null)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.Query(sql, param).ToList();
            }
        }

        /// 
        /// 查询第一个数据
        /// 
        /// 
        /// 
        /// 
        public static T QueryFirst(string sql, object param = null)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.Query(sql, param).ToList().First();
            }
        }

        /// 
        /// 查询第一个数据没有返回默认值
        /// 
        /// 
        /// 
        /// 
        public static T QueryFirstOrDefault(string sql, object param = null)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.Query(sql, param).ToList().FirstOrDefault();
            }
        }

        /// 
        /// 查询单条数据
        /// 
        /// 
        /// 
        /// 
        public static T QuerySingle(string sql, object param = null)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.Query(sql, param).ToList().Single();
            }
        }

        /// 
        /// 查询单条数据没有返回默认值
        /// 
        /// 
        /// 
        /// 
        public static T QuerySingleOrDefault(string sql, object param = null)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.Query(sql, param).ToList().SingleOrDefault();
            }
        }

        /// 
        /// 增删改
        /// 
        /// 
        /// 
        /// Number of rows affected
        public static int Execute(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.Execute(sql, param);
            }
        }

        /// 
        /// Reader获取数据
        /// 
        /// 
        /// 
        /// 
        public static IDataReader ExecuteReader(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.ExecuteReader(sql, param);
            }
        }

        /// 
        /// Scalar获取数据
        /// 
        /// 
        /// 
        /// 
        public static object ExecuteScalar(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.ExecuteScalar(sql, param);
            }
        }

        /// 
        /// Scalar获取数据
        /// 
        /// 
        /// 
        /// 
        public static T ExecuteScalarForT(string sql, object param)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                return con.ExecuteScalar(sql, param);
            }
        }

        /// 
        /// 带参数的存储过程
        /// 
        /// 
        /// 
        /// 
        public static List ExecutePro(string proc, object param)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                List list = con.Query(proc,
                    param,
                    null,
                    true,
                    null,
                    CommandType.StoredProcedure).ToList();
                return list;
            }
        }


        /// 
        /// 事务1 - 全SQL
        /// 
        /// 多条SQL
        /// param
        /// 
        public static int ExecuteTransaction(string[] sqlarr)
        {
            using (SqlConnection con = new SqlConnection(connectionString))
            {
                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;
                    }
                }
            }
        }

        /// 
        /// 事务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(connectionString))
            {
                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;
                    }
                }
            }
        }
    }
}

调用方法:

//查 无参数
var list = DapperHelper.Query("select * from T_User ").ToList();
//查 带参数
var list = DapperHelper.Query("select * from T_User where uid=@uid", new { uid = 1, }).ToList();
//增
int ins = DapperHelper.Execute("insert into T_User (uid,username) value(@uid,@username)", new { uid = 1, username = "张三" });
//改
int upd = DapperHelper.Execute("update T_User set username=@username where uid=@uid", new { username = "李四", uid = 1});
//删
int del = DapperHelper.Execute("delete from T_User where uid=@uid", new { uid = 1 });

Dapper.DLL文件下载地址:https://download.csdn.net/download/djk8888/11824502

你可能感兴趣的:(ASP.NET代码)