static async Task <int> Main(string[] args) { Console.WriteLine(string.Format("Time {0} - Starting CSV Importer ...", DateTime.Now.ToString())); CsvImporterDbContext contextStock = new CsvImporterDbContext(); var db = contextStock.Database.CanConnect(); if (db) { Console.WriteLine("Succesful connection to database."); } else { Console.WriteLine("Not possible to connect to database, check your configuration."); return(0); } try { var result = await DownloadFileStockAsync(); if (result) { Console.WriteLine(string.Format("Time {0} - Download complete.", DateTime.Now.ToString())); var resultProcess = await ProcessStockAsync(); if (resultProcess) { Console.WriteLine(string.Format("Time: {0} - Stock Process succesful.", DateTime.Now.ToString())); return(1); } } return(0); } catch (Exception ex) { Console.WriteLine(ex.Message); return(-1); } }
/// <summary> /// Process a file local to Table Stock /// </summary> /// <returns></returns> private static async Task <bool> ProcessStockAsync() { var fileLocal = GetFilePath(); if (File.Exists(fileLocal)) { CsvImporterDbContext contextStock = new CsvImporterDbContext(); #region Delete all table content do { Console.WriteLine("Deleting data older..."); var listDelete = contextStock.Stock.Take(10000).ToList(); contextStock.BulkDelete(listDelete); } while (contextStock.Stock.Count() != 0); #endregion List <Stock> listStockStore = new List <Stock>(); int countRow = 0; Console.WriteLine("Processing file...take several minutes, please wait."); using (StreamReader sr = new StreamReader(fileLocal)) { string line; while ((line = sr.ReadLine()) != null) { if (countRow != 0) { string[] data = line.Split(";"); Stock newData = new Stock { PointOfSale = data[0], Product = data[1], Date = Convert.ToDateTime(data[2]), AvailableQuantity = Convert.ToInt32(data[3]) }; listStockStore.Add(newData); } countRow++; } } if (countRow != 0) { Console.WriteLine(string.Format("{0} data will be inserted, please wait a fiew minutes.", countRow - 1)); await contextStock.BulkInsertAsync(listStockStore); return(true); } return(false); } else { Console.WriteLine("Not found the file, abort process."); return(false); } }