示例#1
0
        /// <summary>
        /// 清空数据库
        /// </summary>
        public void ClearLotteryDB()
        {
            using (var lotterysEntities = new LotterysEntities())
            {
                foreach (Lottery lottery in lotterysEntities.Lotterys)
                {
                    lotterysEntities.Lotterys.Remove(lottery);
                }

                lotterysEntities.SaveChanges();
            }
        }
        private void loadLotteryData(int perPage, int pageNo)
        {
            using (var lotterysEntities = new LotterysEntities())
            {
                var ls = lotterysEntities.Lotterys.OrderByDescending(l => l.ID).Skip(perPage * (pageNo - 1)).Take(perPage).Select(l => l).ToList();

                this.lswLotterys.ItemsSource = ls;
            }
        }
        private void initPageData(int perPage)
        {
            using (var lotterysEntities = new LotterysEntities())
            {
                int count = lotterysEntities.Lotterys.Count();

                int page = count / perPage;

                if (count % perPage > 0)
                {
                    page++;
                }

                this.lbCurrentPage.Content = 1;
                this.lbPageCount.Content = page;
                this.lbResultCount.Content = count;
            }
        }
        /// <summary>
        /// 初始加载数据
        /// </summary>
        private void loadHistoryLottery(int id, LotterysEntities lotterysEntities)
        {
            int initId = id;

            LotteryFetcher fetcher = LotteryFetcher.GetInstance();

            Lottery lottery = fetcher.fetchLottery(initId);

            if (lottery == null && initId < ((DateTime.Now.Year + 1) * 1000))
            {
                initId /= 1000;
                initId *= 1000;
                initId += 1001;

                lottery = fetcher.fetchLottery(initId);
            }

            while (lottery != null)
            {
                Console.WriteLine(initId);

                lotterysEntities.Lotterys.Add(lottery);
                initId++;

                lottery = fetcher.fetchLottery(initId);

                if (lottery == null && initId < ((DateTime.Now.Year + 1) * 1000))
                {
                    initId /= 1000;
                    initId *= 1000;
                    initId += 1001;

                    lottery = fetcher.fetchLottery(initId);
                }

            }
        }
        /// <summary>
        /// 初始化DB
        /// </summary>
        private void initDB()
        {
            using (var lotterysEntities = new LotterysEntities())
            {

                int initialId = int.Parse(ConfigurationManager.AppSettings["initialId"]);

                // 非空库
                if (lotterysEntities.Lotterys.Count<Lottery>() != 0)
                {

                    initialId = lotterysEntities.Lotterys.Max(l => l.ID) + 1;

                }

                loadHistoryLottery(initialId, lotterysEntities);

                lotterysEntities.SaveChanges();

            }
        }