2019独角兽企业重金招聘Python工程师标准>>>
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
{
///
/// Dapper Helper
/// create by ben.jiang 2017/5/21
///
public partial class DapperDataAsync
{
///
/// DB Connetction String
///
private static string connectionString = ConfigurationManager.ConnectionStrings["Movitech"].ToString();
///
/// Get Entity (int key)
///
public static async Task GetAsync(int id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Get Entity (long key)
///
public static async Task GetAsync(long id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Get Entity (guid key)
///
public static async Task GetAsync(System.Guid id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Get Entity (string key)
///
public static async Task GetAsync(string id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.GetAsync(id, transaction, commandTimeout);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Get All List
///
public static async Task> GetAllAsync() where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.GetAllAsync();
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Get List With SQL
///
public static async Task> GetListAsync(string sql) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.QueryAsync(sql, commandType: CommandType.Text);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Insert Entity
///
public static async Task InsertAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
return await conn.InsertAsync(model, transaction, commandTimeout);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return 0;
}
}
///
/// Update Entity
///
public static async Task UpdateAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
bool b = await conn.UpdateAsync(model, transaction, commandTimeout);
if (b) { return model; }
else { return null; }
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
/// Delete Entity
///
public static async Task DeleteAsync(T model, IDbTransaction transaction = null, int? commandTimeout = null) where T : class, new()
{
try
{
using (var conn = new SqlConnection(connectionString))
{
bool b = await conn.DeleteAsync(model, transaction, commandTimeout);
if (b) { return model; }
else { return null; }
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
///
///Execute SQL Statement
///
public static async Task ExecSqlAsync(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
///
/// Get Entity By ProcInstID
///
public static async Task GetByProcInstIdAsync(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(sql);
}
}
catch (Exception ex)
{
SystemLog.WriteError(ex);
return null;
}
}
#endregion
}
}