123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- 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<Entity>
- where Entity : ModelBase, new()
- {
- public bool bSuccess = false;
- protected string Code = "";
- public Int32 Rows = 0;
- public string Message = "";
- /// <summary>
- /// 源列表
- /// </summary>
- public List<Entity> Entities = null;
- /// <summary>
- /// 查找源数据
- /// </summary>
- /// <param name="t">源实例</param>
- /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
- /// <returns>List<T>/></returns>
- protected List<Entity> Query(string strSql, Entity entity = null)
- {
- string fields = FieldNickGenerator<Entity>.GeneratFieldNick().ToString();
- strSql = string.Format(strSql, fields);
- ConnectorManager cm = new ConnectorManager();
- SqlConnection sql = cm.GetInstance();
- try
- {
- this.Entities = sql.Query<Entity>(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;
- }
- /// <summary>
- /// 查找源数据
- /// </summary>
- /// <param name="t">源实例</param>
- /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
- /// <returns>List<T>/></returns>
- 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();
- }
- /// <summary>
- /// 查找源数据
- /// </summary>
- /// <param name="t">源实例</param>
- /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
- /// <returns>List<T>/></returns>
- 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();
- }
- /// <summary>
- /// 查找源数据
- /// </summary>
- /// <param name="t">源实例</param>
- /// <param name="bAll"> 是否查询所有:true 为所有,false 为带条件 , 默认为:false </param>
- /// <returns>List<T>/></returns>
- 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();
- }
- }
- }
|