示例#1
0
 public MJsonData(object data)
 {
     if (data is Exception)
     {
         _error = (data as Exception).Message;
     }
     else
     {
         Data = data;
         if (data is IPaginationList)
         {
             IPaginationList pl = (IPaginationList)data;
             this.Pos = pl.StartPos;
             if (this.Pos < 0)
             {
                 this.Pos = 0;
             }
             /*if (pl.StartPos == 0)*/
             this.Total_count = pl.AllCount;
         }
         else if (data is MTreeList)
         {
             Parent = ((MTreeList)data).Parent;
         }
     }
 }
示例#2
0
文件: MEDMSql.cs 项目: PANKOVAN/TEST
        public IList Select(IList list, Type t, string sql, params object[] parms)
        {
            SqlConnection con = ConnectionPool.GetConnection();

            try
            {
                parms = GetParamList(parms);
                int             allCount = -1;
                IPaginationList plist    = null;
                if (list is IPaginationList)
                {
                    plist = (IPaginationList)(list);
                }

                DateTime   begtime = DateTime.Now;
                SqlCommand com     = new SqlCommand(sql, con);
                for (int i = 0; i < parms.Length - 1; i += 2)
                {
                    if (parms[i] != null)
                    {
                        com.Parameters.Add(new SqlParameter(parms[i].ToString(), PrepareParam(parms[i + 1])));
                    }
                }
                // Для PaginationList select выполняем в 2 этапа сначала считаем кол-во а потом сам select c добавленным top
                if (plist != null)
                {
                    if (plist.Top != "")
                    {
                        string sql1 = com.CommandText;
                        int    i    = sql1.IndexOf('*');
                        int    j    = sql1.ToLower().IndexOf("order by");
                        if (i > 0 && (j < 0 || i < j))
                        {
                            com.CommandText = sql1.Remove(j).Remove(i, 1).Insert(i, "count(*)");
                            allCount        = Convert.ToInt32(com.ExecuteScalar());
                            com.CommandText = sql1.Insert(i, plist.Top + " ");
                        }
                    }
                }
                bool f = MainDic.ContainsKey(t);
                using (SqlDataReader rdr = com.ExecuteReader())
                {
                    while (rdr.Read())
                    {
                        if (plist != null)
                        {
                            if ((plist.AllCount >= plist.StartPos || plist.StartPos == -1) && plist.AllCount < plist.StartPos + plist.PageSize || plist.StartPos == -1 || plist.PageSize == -1)
                            {
                                MObj obj = null;
                                if (f)
                                {
                                    obj = MainDic.CreateObj(t);
                                }
                                else
                                {
                                    obj = Activator.CreateInstance(t) as MObj;
                                    if (obj == null)
                                    {
                                        throw new Exception($@"Тип ""{t}"" не наследует от MObj");
                                    }
                                    obj.Model = this;
                                }
                                SetObjValues(obj, rdr);
                                if (obj is MEDMObj)
                                {
                                    MainDic.AddObj(obj as MEDMObj);
                                }
                                if (list != null)
                                {
                                    list.Add(obj);
                                }
                            }
                            plist.AllCount++;
                        }
                        else
                        {
                            MObj obj = null;
                            if (f)
                            {
                                obj = MainDic.CreateObj(t);
                            }
                            else
                            {
                                obj = Activator.CreateInstance(t) as MObj;
                                if (obj == null)
                                {
                                    throw new Exception($@"Тип ""{t}"" не наследует от MObj");
                                }
                                obj.Model = this;
                            }
                            SetObjValues(obj, rdr);
                            if (obj is MEDMObj)
                            {
                                MainDic.AddObj(obj as MEDMObj);
                            }
                            if (list != null)
                            {
                                list.Add(obj);
                            }
                        }
                    }
                }
                if (plist != null && allCount >= 0)
                {
                    plist.AllCount = allCount;
                }
                MainTrace.Add(TraceType.Sql, $"SELECT => {com.CommandText}");
            }
            finally
            {
                ConnectionPool.FreeConnection(con);
            }
            return(list);
        }