Dapper Helper

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

展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值