Beispiel #1
0
        public ExeResEdm GetListByPage(string tableName, PageSerach <T> para, DBOperUser dbLogMsg = null)
        {
            var orderByStr = LambdaToSqlHelper <T> .GetSqlFromLambda(para.OrderBy).OrderbySql;

            string whereSql = !string.IsNullOrEmpty(para.StrWhere) ? para.StrWhere : LambdaToSqlHelper <T> .GetWhereFromLambda(para.Filter, DBStoreType.NoSelect);

            SearchParam searchParam = new SearchParam()
            {
                Orderby = orderByStr, PageIndex = para.PageIndex, PageSize = para.PageSize, TableName = tableName, StrWhere = whereSql,
            };
            ExeResEdm res    = GetDTByPage(searchParam);
            int       curNum = 0;

            if (res.ErrCode == 0)
            {
                List <T> list = DtModelConvert <T> .DatatableToList((res.ExeModel as DataTable));

                res.ExeModel = list.AsQueryable();
                curNum       = list.Count();
                res.ExeNum   = searchParam.TotalCount;
            }
            WriteLogMsg(dbLogMsg, LogType.查询, "根据[" + DtModelConvert <T> .SerializeToString(searchParam) + "]获取了分页数据,返回了"
                        + curNum + "/" + searchParam.TotalCount + "条记录", tableName);
            return(res);
        }
Beispiel #2
0
        public ExeResEdm Update(string tableName, List <T> list, List <Dictionary <string, string> > updateFDList, DBOperUser dbLogMsg = null, string strComFields = "*")
        {
            var dt  = GetDataTableComplex(tableName, null, list, updateFDList);
            var res = UpdateDtToDB(dt, strComFields);

            WriteLogMsg(dbLogMsg, LogType.批量修改, "参数为:" + DtModelConvert <object> .SerializeToString(new { Data = list, UpdateFDs = updateFDList }) + ",受影响的行数为" + res.ExeNum, tableName);
            return(res);
        }
Beispiel #3
0
        public ExeResEdm Add(string tableName, List <T> list, DBOperUser dbLogMsg = null, string strComFields = "*")
        {
            var dt  = GetDataTable(tableName, null, list, null);
            var res = UpdateDtToDB(dt, strComFields);

            WriteLogMsg(dbLogMsg, LogType.批量插入, "参数为:" + DtModelConvert <List <T> > .SerializeToString(list) + ",受影响的行数为" + res.ExeNum, tableName);
            return(res);
        }
Beispiel #4
0
        public ExeResEdm AddUpdateDelete(DBOperUser dbLogMsg = null, params AddUpdateDelEdm[] datas)
        {
            DataSet ds = new DataSet();

            if (datas == null || datas.Length <= 0)
            {
                return(new ExeResEdm()
                {
                    ErrCode = 1, Module = "AddUpdateDelete方法", ErrMsg = "没有有效的参数",
                });
            }
            datas = datas.Where(a => a.Datas != null && a.Datas.Count > 0).ToArray();
            if (datas.Length <= 0)
            {
                return(new ExeResEdm()
                {
                    ErrCode = 1, Module = "AddUpdateDelete方法", ErrMsg = "没有有效的参数",
                });
            }
            var models = datas.ToList();

            //若TableName相同,则合并
            models = models.Select(a => { a.TableName = a.TableName.Trim().ToLower().Trim(); return(a); }).ToList();
            var groups = models.GroupBy(a => a.TableName).ToList();
            List <AddUpdateDelEdm>      realModels = new List <AddUpdateDelEdm>();
            Dictionary <string, string> dtPKs      = new Dictionary <string, string>();

            models.Clear();
            foreach (var item in groups)
            {
                AddUpdateDelEdm cur = new AddUpdateDelEdm()
                {
                    TableName = item.Key,
                };
                cur.Datas    = new List <object>();
                cur.UpdateFD = new List <Dictionary <string, string> >();
                var    itemList = item.ToList();
                bool[] bAdd     = new bool[itemList.Count];
                for (int i = 0; i < itemList.Count; i++)
                {
                    var m = itemList[i];
                    cur.MainFields = !string.IsNullOrEmpty(m.MainFields) ? m.MainFields : cur.MainFields;
                    if (m.UpdateFD != null && m.UpdateFD.Count == m.Datas.Count)
                    {
                        cur.Datas.AddRange(m.Datas);
                        cur.UpdateFD.AddRange(m.UpdateFD);
                        bAdd[i] = false;
                    }
                    else
                    {
                        foreach (var d in m.Datas)
                        {
                            cur.Datas.Add(d);
                            if (m.UpdateFD == null || m.UpdateFD.Count <= 0)
                            {
                                //  cur.UpdateFD.Add(new Dictionary<string, string>() { });
                                bAdd[i] = true;
                            }
                            else if (m.UpdateFD.Count == 1)
                            {
                                cur.UpdateFD.Add(m.UpdateFD[0]);
                                bAdd[i] = false;
                            }
                        }
                    }
                    dtPKs.Add(cur.TableName, cur.MainFields);
                }
                if (bAdd.Distinct().Count() != 1)
                {
                    ExeResEdm exeResEdm = new ExeResEdm()
                    {
                        ErrCode = 1,
                        ExBody  = new Exception("同一张表[" + item.Key + "],不能同时有添加和更新操作"),
                        Module  = "AddUpdate 方法",
                    };
                    return(exeResEdm);
                }
                models.Add(cur);
            }

            //获取所有的datatable模板
            List <SqlContianer> ltSqls = models.Select(a => new SqlContianer()
            {
                tableName = a.TableName, strSqlTxt = GetColumnsNameSql(a.TableName)
            }).ToList();
            var dsTemplate = GetDataSets(ltSqls).ExeModel as DataSet;

            if (dsTemplate == null || dsTemplate.Tables.Count <= 0)
            {
                return(new ExeResEdm()
                {
                    ErrCode = 1, ErrMsg = "未获取到数据", Module = "AddUpdateDelete方法"
                });
            }
            foreach (var item in models)
            {
                DataTable dt         = new DataTable();
                DataTable dtTemplate = dsTemplate.Tables[item.TableName];
                if (item.UpdateFD == null || item.UpdateFD.Count == 0)
                {
                    dt = GetDataTable(item.TableName, dtTemplate, item.Datas);
                }
                else if (item.UpdateFD.Count == 1)
                {
                    dt = GetDataTable(item.TableName, dtTemplate, item.Datas, item.UpdateFD[0]);
                }
                else
                {
                    dt = GetDataTableComplex(item.TableName, dtTemplate, item.Datas, item.UpdateFD);
                }
                ds.Tables.Add(dt);
            }

            var res = UpdateDsToDB(ds, dtPKs);

            WriteLogMsg(dbLogMsg, LogType.批量增删改, "参数为:" + DtModelConvert <List <AddUpdateDelEdm> > .SerializeToString(models) + ",受影响的行数为" + res.ExeNum, "AddUpdateDelete方法");
            return(res);
        }