private static void GetSubListDailyQuotes(List <string> subList, int index)
        {
            for (int i = 0; i < subList.Count; i++)
            {
                string            info           = $"T{index}=>{i}: ";
                List <QuoteDaily> dailyQuoteList = GUIDataHelper.GetQuoteDailyList(subList[i]);
                TimeSpan          timeConsume    = new TimeSpan();
                using (DbStockMonitor dbctx = new DbStockMonitor())
                {
                    try
                    {
                        DateTime start = DateTime.Now;
                        dailyQuoteList.ForEach(p => dbctx.QuoteDailies.Add(p));
                        dbctx.SaveChanges();
                        DateTime end = DateTime.Now;
                        timeConsume = end - start;
                    }
                    catch (SystemException ex)
                    {
                        Console.Out.WriteLine(info + "!!!! DB save changes failure: " + subList[i] + ", " + ex.Message);
                    }

                    Console.Out.WriteLine(
                        $"{info}Insert {subList[i]}, from {dailyQuoteList[0].Date} to {dailyQuoteList[dailyQuoteList.Count - 1].Date}, total: {dailyQuoteList.Count}, time: {timeConsume.TotalSeconds} sec");
                }
            }
        }
        public static void FirstImportStockListToDatabase()
        {
            int counter = 0;

            using (DbStockMonitor context = new DbStockMonitor())
            {
                counter = context.Companies.Count();
                Console.Out.WriteLine($"Counter start: {counter}");
            }

            List <FmgStockListEntity> stockList = RetrieveJsonDataHelper.RetrieveStockList();

            Console.Out.WriteLine("***Length of list: " + stockList.Count + "\n\n");
            for (int i = 0; i < 5000; i++)
            {
                Console.Out.Write($"{i}: ");
                string  symbol  = stockList[i].Symbol;
                Company company = null;
                try
                {
                    company = GUIDataHelper.GetCompanyBySymbol(symbol);
                }
                catch (Newtonsoft.Json.JsonSerializationException ex)
                {
                    Console.Out.WriteLine("!!!!! Failed: " + ex.Message + $" <{symbol}> ");
                }
                catch (ArgumentException ex)
                {
                    Console.Out.WriteLine("!!!!! Failed: " + ex.Message + $" <{symbol}> ");
                }
                catch (SystemException ex)
                {
                    Console.Out.WriteLine("!!!!! Failed: " + ex.Message + $" <{symbol}> ");
                }

                if (company == null)
                {
                    continue;
                }

                using (DbStockMonitor dbStockContext = new DbStockMonitor())
                {
                    dbStockContext.Companies.Add(company);
                    try
                    {
                        dbStockContext.SaveChanges();
                        Console.Out.WriteLine($">>>>> {++counter}: Successfully insert record: <{company.Symbol}>");
                    }
                    catch (SystemException ex)
                    {
                        Console.Out.WriteLine($"!!!!! Database save changes exception! <{company.Symbol}>, " + ex.Message);
                    }
                }
            }
        }
Exemple #3
0
        // private static DbStockMonitor _dbContext = new DbStockMonitor();

        public static void InsertCompanyToDb(string symbol)
        {
            Company company = GUIDataHelper.GetCompanyBySymbol(symbol);

            try
            {
                using (DbStockMonitor _dbContext = new DbStockMonitor())
                {
                    _dbContext.Companies.Add(company);
                    _dbContext.SaveChanges();
                    Console.Out.WriteLine(company.ToString());
                }
            }
            catch (Exception ex)
            {
                throw new SystemException($"InsertCompanyToDb exception: {symbol} > {ex.Message}");
            }
        }