private static DSLDailySales ChangString(string line) { DSLDailySales dailSales = new DSLDailySales(); // var searchstore = SearchStore(); string storecode = ""; try { //Common.WriteLog("切割字串 " + line); //去除多空白 line = new Regex("[\\s]+").Replace(line, " "); //空白切割 string[] words = line.Split('|'); //string[] words = Split2(line + "|", "|").ToArray(); storecode = searchstore.Where(x => x.Store == words[0]).Select(x => x.StoreCode).FirstOrDefault(); //if (Array.IndexOf(itemcode, words[2]) < 0 || Array.IndexOf(dept, words[1]) < 0) //{ // return dailSales; //} dailSales = new DSLDailySales { StoreCode = storecode, DepartmentCode = words[1], ItemCode = words[2], SubCode = words[3], SalesDate = words[4], UnitCode = words[5], StatusPromotion = words[6], SPType = words[7], VatRate = words[8], SalesQty = words[9], SalesAmount = words[10], SalesPrice = words[11], PurchasePrice = words[12], Rebate = words[13], VatAmount = words[14], Input = words[15], OriginalSellingPrice = "0", CSTAmount = "0" }; return(dailSales); } catch (Exception ex) { Common.WriteLog("切割字串 " + line + " 失敗"); Common.WriteLog(ex.ToString()); return(dailSales); } }
public static bool StartInsert(string path, string finame) { bool result = true; DSLDailySales daily = new DSLDailySales(); List <DSLDailySales> sales = new List <DSLDailySales>(); string line; try { //使用FileStream讀取檔案 FileStream fileStream = File.OpenRead(path); StreamReader reader = new StreamReader(fileStream, Encoding.UTF8); string filename = Path.GetFileName(finame).Substring(0, 3); int i = 0; line = reader.ReadLine(); Common.WriteLog("切割字串"); //Console.WriteLine("切割字串"); //start = DateTime.Now; while (line != null) { if (i == 0) { line = reader.ReadLine(); } line = new Regex("[\\s]+").Replace(line, " "); if (line == " " || line == "") { line = reader.ReadLine(); continue; } daily = ChangString(line); if (daily.DepartmentCode != null) { sales.Add(daily); if (i % 100000 == 0) { result = InsertToSql(sales); sales = new List <DSLDailySales>(); if (!result) { return(false); } } } //Read the next line line = reader.ReadLine(); i++; } //end = DateTime.Now; //Console.WriteLine(start.ToString("hh:mm:ss")); //Console.WriteLine(end.ToString("hh:mm:ss")); reader.Close(); Common.WriteLog("新增DB"); result = InsertToSql(sales); Common.WriteLog("新增 " + finame + " 成功,一共 " + i + " 筆"); Console.WriteLine("新增 " + finame + " 成功,一共 " + i + " 筆"); return(result); } catch (Exception ex) { result = false; Common.WriteLog(ex.ToString()); return(result); } }