using HNWD.Pregrant.Model; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using Dapper; namespace HNWD.Pregrant.DataAccess { public abstract class GenericDataAccess where Entity : ModelBase, new() { public bool bSuccess = false; protected string Code = ""; public Int32 Rows = 0; public string Message = ""; /// /// 源列表 /// public List Entities = null; /// /// 查找源数据 /// /// 源实例 /// 是否查询所有:true 为所有,false 为带条件 , 默认为:false /// List/> protected List Query(string strSql, Entity entity = null) { string fields = FieldNickGenerator.GeneratFieldNick().ToString(); strSql = string.Format(strSql, fields); ConnectorManager cm = new ConnectorManager(); SqlConnection sql = cm.GetInstance(); try { this.Entities = sql.Query(strSql, entity).ToList(); this.bSuccess = true; this.Rows = this.Entities.Count; this.Code = "OK!"; this.Message = "成功获取请求数据!"; } catch (Exception ex) { this.bSuccess = false; this.Code = "ERROR!"; this.Rows = 0; this.Message = ex.Message; } finally { sql.Close(); } return this.Entities; } /// /// 查找源数据 /// /// 源实例 /// 是否查询所有:true 为所有,false 为带条件 , 默认为:false /// List/> protected void Execute(string strSql, Entity entity) { ConnectorManager cm = new ConnectorManager(); SqlConnection sql = cm.GetInstance(); IDbTransaction transaction = sql.BeginTransaction(); try { int i = sql.Execute(strSql, entity, transaction, null, null); this.bSuccess = true; this.Code = "OK!"; this.Message = "命令执行成功!"; this.Rows = 0; transaction.Commit(); } catch (Exception ex) { this.bSuccess = false; this.Code = "ERROR!"; this.Rows = 0; this.Message = ex.Message; transaction.Rollback(); } finally { sql.Close(); } transaction.Dispose(); } /// /// 查找源数据 /// /// 源实例 /// 是否查询所有:true 为所有,false 为带条件 , 默认为:false /// List/> protected void Execute(string isql, string usql, string dsql) { ConnectorManager cm = new ConnectorManager(); SqlConnection sql = cm.GetInstance(); IDbTransaction transaction = sql.BeginTransaction(); try { int i = 0, u = 0, d = 0; if (isql.Length > 0) { i = sql.Execute(isql, null, transaction, null, null); } if (usql.Length > 0) { u = sql.Execute(usql, null, transaction, null, null); } if (dsql.Length > 0) { d = sql.Execute(dsql, null, transaction, null, null); } this.bSuccess = true; this.Code = "OK!"; this.Message = "命令执行成功!(i:" + i.ToString() + ",u:" + u.ToString() + ",d:" + d.ToString() + ")"; this.Rows = i; transaction.Commit(); } catch (Exception ex) { this.bSuccess = false; this.Code = "ERROR!"; this.Rows = 0; this.Message = ex.Message; transaction.Rollback(); } finally { sql.Close(); } transaction.Dispose(); } /// /// 查找源数据 /// /// 源实例 /// 是否查询所有:true 为所有,false 为带条件 , 默认为:false /// List/> protected void Execute(string isql, string usql, string dsql, string asql) { ConnectorManager cm = new ConnectorManager(); SqlConnection sql = cm.GetInstance(); IDbTransaction transaction = sql.BeginTransaction(); try { int i = 0, u = 0, d = 0; if (isql.Length > 0) { i = sql.Execute(isql, null, transaction, null, null); } if (usql.Length > 0) { u = sql.Execute(usql, null, transaction, null, null); } if (dsql.Length > 0) { d = sql.Execute(dsql, null, transaction, null, null); } sql.Execute(asql, null, transaction, null, null); this.bSuccess = true; this.Code = "OK!"; this.Message = "命令执行成功!(i:" + i.ToString() + ",u:" + u.ToString() + ",d:" + d.ToString() + ")"; this.Rows = i; transaction.Commit(); } catch (Exception ex) { this.bSuccess = false; this.Code = "ERROR!"; this.Rows = 0; this.Message = ex.Message; transaction.Rollback(); } finally { sql.Close(); } transaction.Dispose(); } } }