新文章 网摘 文章 随笔 日记

SqlSugar通用的执行存储过程方法(以out方式返回参数)

        public void ExecuteProcedure(
            string sprocName,
            Dictionary<string, object> intParams,
            Dictionary<string, object> outParams)
        {
            List<SugarParameter> pList = new List<SugarParameter>();
            if (intParams != null)
            {
                pList = intParams.Select(
                    obj => new SugarParameter($@"@{obj.Key}", obj.Value))
                    .ToList();


            }
            if (outParams != null)
                pList.AddRange(
                    outParams.Select(
                        obj => new SugarParameter($@"@{obj.Key}", obj.Value)
                        {
                            Direction = ParameterDirection.Output
                        }));
            _unitOfWork.Db.Ado.UseStoredProcedure().ExecuteCommand(sprocName, pList);
            foreach (var p in pList.Where(r => r.Direction == ParameterDirection.Output))
            {
                var pName = p.ParameterName.Substring(1);
                if (outParams != null && outParams.ContainsKey(pName))
                {
                    outParams[pName] = p.Value;
                }

            }
        }

 

posted @ 2022-06-17 16:08  岭南春  阅读(3263)  评论(1)    收藏  举报