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 ;
}
}
/// <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 ;
}
}
#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
}
}