Пример #1
0
 public static int Insert(List <Areas> areas)
 {
     using (MeiTuanEntities db = new MeiTuanEntities())
     {
         var query = from a in db.T_Areas
                     where a.State == true
                     select a;
         List <Areas> list = new List <Areas>();
         //默认值
         for (int i = 0; i < areas.Count; i++)
         {
             areas[i].State   = true;
             areas[i].P_ID    = 0;
             areas[i].AddTime = DateTime.Now;
             var subAreas = areas[i].subAreas;
             for (int j = 0; j < subAreas.Count; j++)
             {
                 subAreas[j].State   = true;
                 subAreas[j].P_ID    = areas[i].ID;
                 subAreas[j].AddTime = DateTime.Now;
             }
             subAreas.Remove(subAreas.Find(m => m.Name.Equals("全部")));
             list.AddRange(subAreas);
             //db.T_Areas.AddRange(subAreas);
         }
         list.AddRange(areas);
         //var temp_areas = db.T_Areas.AddRange(areas);
         List <int> list_id_source = query.Select(m => m.ID).ToList();
         List <int> list_id_target = list.Select(m => m.ID).ToList();
         //找出需要插入的值
         List <int> list_id_insert = list_id_target.Except(list_id_source).Distinct().OrderBy(m => m).ToList();
         var        list_inserted  = list.FindAll(m => list_id_insert.Contains(m.ID)).Select(i => {
             return(new T_Areas
             {
                 ID = i.ID,
                 Name = i.Name,
                 URL = i.URL,
                 P_ID = i.P_ID,
                 State = i.State,
                 AddTime = i.AddTime,
             });
         });
         foreach (var item in list_inserted)
         {
             db.Database.ExecuteSqlCommand("INSERT INTO T_Areas(ID,Name,URL,P_ID,State,AddTime) VALUES(@ID,@Name,@URL,@P_ID,@State,@AddTime)",
                                           new SqlParameter("@ID", item.ID),
                                           new SqlParameter("@Name", item.Name),
                                           new SqlParameter("@URL", item.URL),
                                           new SqlParameter("@P_ID", item.P_ID),
                                           new SqlParameter("@State", item.State),
                                           new SqlParameter("@AddTime", item.AddTime));
         }
         db.SaveChanges();
         return(list_inserted.Count());
     }
 }
Пример #2
0
 public int Delete()
 {
     using (MeiTuanEntities db = new MeiTuanEntities())
     {
         return(db.Database.ExecuteSqlCommand(@" TRUNCATE TABLE T_Areas
                         TRUNCATE TABLE T_Cate
                         TRUNCATE TABLE T_DinnerCountsAttr
                         TRUNCATE TABLE T_SortTypesAttr"));
     }
 }
Пример #3
0
 public void MyTestMethod2()
 {
     using (MeiTuanEntities db = new MeiTuanEntities())
     {
         var query = from a in db.T_City
                     where a.State == true
                     select a;
         var list = query.ToList();
     }
 }
Пример #4
0
        private void 更新美食模块ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //从数据中取出URL,然后访问网页,解析网页获取到美食菜单的URL,访问并记录该URL,
            //进入美食列表页后,依次访问不同的分类,将美食的名称,评分,地址,人均消费记录到数据库中
            numberOfMeishi = 0;
            listBox1.Items.Add($"{DateTime.Now.ToShortTimeString()} 开始抓取数据...");
            HtmlWeb webClient = new HtmlWeb();

            try
            {
                _cancel_token = new CancellationTokenSource();
                using (MeiTuanEntities db = new MeiTuanEntities())
                {
                    var query = from a in db.T_City
                                where a.State == true
                                select new CityViewModel
                    {
                        ID        = a.ID,
                        Name      = a.Name,
                        URL       = a.URL,
                        MeiShiURL = a.MeiShiURL
                    };

                    var  list   = query.ToList();
                    int  offset = list.Count / 4;
                    Task task   = new Task(() => {
                        var list_offset = list.Take(offset).ToList();
                        InsertMeiShi(list_offset);
                    });
                    task.Start();
                    Task task2 = new Task(() => {
                        var list_offset = list.Skip(offset).Take(offset).ToList();
                        InsertMeiShi(list_offset);
                    });
                    task2.Start();
                    Task task3 = new Task(() => {
                        var list_offset = list.Skip(offset * 2).Take(offset).ToList();
                        InsertMeiShi(list_offset);
                    });
                    task3.Start();
                    Task task4 = new Task(() => {
                        var list_offset = list.Skip(offset * 3).ToList();
                        InsertMeiShi(list_offset);
                    });
                    task4.Start();
                }
            }
            catch (Exception ex)
            {
                MyLog.logger.Debug(ex.Message);
            }
        }
Пример #5
0
 /// <summary>
 /// 导入数据
 /// </summary>
 public static int Insert(List <SortTypesAttr> cate)
 {
     using (MeiTuanEntities db = new MeiTuanEntities())
     {
         var query = from a in db.T_SortTypesAttr
                     where a.State == true
                     select a;
         List <SortTypesAttr> list = new List <SortTypesAttr>();
         list.AddRange(cate);
         List <int> list_id_source = query.Select(m => m.ID).ToList();
         List <int> list_id_target = list.Select(m => m.id).ToList();
         //找出需要插入的值
         List <int> list_id_insert = list_id_target.Except(list_id_source).Distinct().OrderBy(m => m).ToList();
         var        list_inserted  = list.FindAll(m => list_id_insert.Contains(m.id)).Select(i => {
             return(new SortTypesAttr
             {
                 id = i.id,
                 name = i.name,
                 url = string.Empty,
                 State = true,
                 AddTime = DateTime.Now
             });
         });
         foreach (var item in list_inserted)
         {
             db.Database.ExecuteSqlCommand("INSERT INTO T_SortTypesAttr(ID,Name,URL,State,AddTime) VALUES(@ID,@Name,@URL,@State,@AddTime)",
                                           new SqlParameter("@ID", item.id),
                                           new SqlParameter("@Name", item.name),
                                           new SqlParameter("@URL", item.url),
                                           new SqlParameter("@State", item.State),
                                           new SqlParameter("@AddTime", item.AddTime));
         }
         db.SaveChanges();
         return(list_inserted.Count());
     }
 }