//path 路徑 finame檔名 public static bool StartInsert(string path, string finame) { bool result = true; int i = 0; string line; List <SUPATT> Sups = new List <SUPATT>(); SUPATT Sup = new SUPATT(); try { Common.WriteLog("開始切割 " + finame); FileStream fileStream = File.OpenRead(path); using (StreamReader reader = new StreamReader(fileStream, Encoding.GetEncoding(0))) { line = reader.ReadLine(); while (line != null) { line = new Regex("[\\s]+").Replace(line, " "); if (line == " " || line == "") { line = reader.ReadLine(); continue; } Sup = splitstrng(line); Sups.Add(Sup); line = reader.ReadLine(); i++; } reader.Close(); } Common.WriteLog("新增DB"); result = InsertToSql(Sups); Common.WriteLog(finame + "切割完成,一共 " + i + " 筆"); return(result); } catch (Exception e) { Common.WriteLog("SUPATTServices失敗"); Common.WriteLog(e.ToString()); result = false; return(result); } }
//切割字串 public static SUPATT splitstrng(string input) { SUPATT Sup = new SUPATT(); try { //切割符號 //string mark = ConfigurationManager.AppSettings["Mark"]; input = new Regex("[\\s]+").Replace(input, " "); //string[] sArray = Regex.Split(input, @"|"); string[] sArray = input.Split('|'); Sup = new SUPATT { StoreCode = sArray[0], AttributeClassCode = sArray[1], AttributeClassDescription = sArray[2], AttributeCode = sArray[3], AttributeCodeDescription = sArray[4], AlphanumericValue = sArray[5], NumberValue = sArray[6], Date = sArray[7], Time = sArray[8], StartDate = sArray[9], EndDate = sArray[10] }; return(Sup); } catch (Exception ex) { Common.WriteLog("切割失敗 " + input + " "); Common.WriteLog(ex.ToString()); return(Sup); throw; } }