/// <summary> /// /// </summary> /// <param name="sqlModel">对象</param> /// <param name="dict">key 为名称,value为值</param> /// <returns></returns> public static Dapper.DynamicParameters GetParams(DDD.Common.SqlModel sqlModel, Dictionary <string, dynamic> dict) { Dapper.DynamicParameters param = new Dapper.DynamicParameters(); //得到sql中的参数集合 List <string> sqlParamList = DDD.Common.StringHelper.GetStringList(sqlModel.commandType, sqlModel.sqlStatement); //遍历xml中的参数集合 List <DDD.Common.SqlParameterModels> listparam = sqlModel.listParameter; if (listparam != null && listparam.Any()) { foreach (DDD.Common.SqlParameterModels item in listparam) { var dictModel = dict.FirstOrDefault(m => m.Key == item.property); if (!string.IsNullOrEmpty(dictModel.Key)) { //处理sql中的参数问题 sqlModel.sqlStatement = DDD.Common.StringHelper.GetStringSql(sqlParamList, sqlModel.sqlStatement, item.property); param.Add(item.column, dictModel.Value, (System.Data.DbType)Enum.Parse(typeof(System.Data.DbType), item.propertyType, true), System.Data.ParameterDirection.Input); } } } return(param); }
/// <summary> /// 通过sql 语句查询一条记录 /// </summary> /// <param name="Id"></param> /// <returns></returns> public async Task <T_Demo> GetModelBysql(Guid Id, string userName) { DDD.Common.SqlModel sqlModel = DDD.Common.SerializationHelper.XmlFileToStringSql("Demo.xml", "GetModel"); //DynamicParameters param = new DynamicParameters(); //param.Add("@Id", Id, System.Data.DbType.Guid, System.Data.ParameterDirection.Input); #region 参数 Dapper.DynamicParameters param = new Dapper.DynamicParameters(); Dictionary <string, dynamic> dict = new Dictionary <string, dynamic>(); dict.Add(nameof(Id), Id); dict.Add(nameof(userName), userName); param = DDD.Common.StringHelper.GetParams(sqlModel, dict); ////得到sql中的参数集合 //List<string> sqlParamList = DDD.Common.StringHelper.GetStringList(sqlModel.commandType,sqlModel.sqlStatement); ////遍历xml中的参数集合 //List<DDD.Common.SqlParameterModels> listparam = sqlModel.listParameter; //if (listparam != null && listparam.Any()) //{ // foreach (DDD.Common.SqlParameterModels item in listparam) // { // if (item.property == nameof(Id)) // { // //处理sql中的参数问题 // sqlModel.sqlStatement = DDD.Common.StringHelper.GetStringSql(sqlParamList, sqlModel.sqlStatement, item.property); // param.Add(item.column, Id, System.Data.DbType.Guid, System.Data.ParameterDirection.Input); // } // } //} #endregion return(await QueryFirstOrDefaultAsync(sqlModel.sqlStatement.Trim(), param)); }