WebEnh

.net7 mvc jquery bootstrap json 学习中 第一次学PHP,正在研究中。自学进行时... ... 我的博客 https://enhweb.github.io/ 不错的皮肤:darkgreentrip,iMetro_HD
随笔 - 1079, 文章 - 1, 评论 - 75, 阅读 - 174万
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

Dapper Helper

Posted on   WebEnh  阅读(1317)  评论(0编辑  收藏  举报
复制代码
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;
using Dapper;
using Dapper.Contrib.Extensions;

namespace Com.BPM.Forms
{
    /// <summary>
    /// Dapper Helper
    /// create by ben.jiang 2017/5/21
    /// </summary>
    public partial class DapperDataAsync
    {
        /// <summary>
        /// DB Connetction String
        /// </summary>
        private static string connectionString = ConfigurationManager.ConnectionStrings["Movitech"].ToString();
        /// <summary>
        /// Get Entity (int key)
        /// </summary> 
        public static async Task<T> GetAsync<T>(int id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.GetAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Get Entity (long key)
        /// </summary> 
        public static async Task<T> GetAsync<T>(long id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.GetAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Get Entity (guid key)
        /// </summary> 
        public static async Task<T> GetAsync<T>(System.Guid id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.GetAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Get Entity (string key)
        /// </summary> 
        public static async Task<T> GetAsync<T>(string id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.GetAsync<T>(id, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Get All List
        /// </summary> 
        public static async Task<IEnumerable<T>> GetAllAsync<T>() where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.GetAllAsync<T>();
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Get List With SQL
        /// </summary> 
        public static async Task<IEnumerable<T>> GetListAsync<T>(string sql) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.QueryAsync<T>(sql, commandType: CommandType.Text);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Insert Entity
        /// </summary>
        public static async Task<int> InsertAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.InsertAsync<T>(model, transaction, commandTimeout);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return 0;
            }
        }
        /// <summary>
        /// Update Entity
        /// </summary>
        public static async Task<T> UpdateAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    bool b = await conn.UpdateAsync<T>(model, transaction, commandTimeout);
                    if (b) { return model; }
                    else { return null; }
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        /// Delete Entity
        /// </summary>
        public static async Task<T> DeleteAsync<T>(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    bool b = await conn.DeleteAsync<T>(model, transaction, commandTimeout);
                    if (b) { return model; }
                    else { return null; }
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        /// <summary>
        ///Execute SQL Statement
        /// </summary> 
        public static async Task<int> ExecSqlAsync<T>(string sql)
        {
            try
            {
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.ExecuteAsync(sql);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return 0;
            }
        }

        #region For Project
        /// <summary>
        /// Get Entity By ProcInstID
        /// </summary> 
        public static async Task<T> GetByProcInstIdAsync<T>(string procInstId) where T : class, new()
        {
            try
            {
                string tbname = typeof(T).Name;
                string sql = string.Format("SELECT * FROM {0} WHERE ProcInstID='{1}'", tbname, procInstId);
                using (var conn = new SqlConnection(connectionString))
                {
                    return await conn.QueryFirstOrDefaultAsync<T>(sql);
                }
            }
            catch (Exception ex)
            {
                SystemLog.WriteError(ex);
                return null;
            }
        }
        #endregion
    }
}
复制代码

 

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

了解更多