zoukankan      html  css  js  c++  java
  • 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
        }
    }
  • 相关阅读:
    几种常见的content-type
    node简单起服务
    ESlint配置案例及如何配置
    网络攻防学习心得一(20159320)工具学习
    网络攻防学习心得一(20159320)黑客信息
    题解 POJ1187 【陨石的秘密】
    题解 POJ1934 【Trip】
    题解 POJ1952 【BUY LOW, BUY LOWER】
    TIM bug 总结以及应对方案
    题解 POJ3171 【Cleaning Shifts】
  • 原文地址:https://www.cnblogs.com/webenh/p/7894646.html
Copyright © 2011-2022 走看看