/// <summary> /// store attendance logs in db /// </summary> /// <param name="t"></param> /// <returns></returns> public static string AttdLogStoreToDb(AttdLog t) { string err = string.Empty; using (SqlConnection cn = new SqlConnection(Utils.Helper.constr)) { try { cn.Open(); string sql = t.GetDBWriteString(); using (SqlCommand cmd = new SqlCommand(sql, cn)) { cmd.ExecuteNonQuery(); } } catch (Exception ex) { if (ex.ToString().Contains("System.Data.SqlClient.SqlException (0x80131904): Violation of PRIMARY KEY")) { } else { err = ex.ToString(); } try { string sql = t.GetDBWriteErrString(); using (SqlCommand cmd = new SqlCommand(sql, cn)) { cmd.ExecuteNonQuery(); //err = "Duplicate Data Found.."; } } catch (Exception ex1) { err += ex1.ToString(); } } } return(err); }
public void ProcessFile(string tpath) { if (File.Exists(tpath)) { var lines = File.ReadLines(tpath); List <AttdLog> listAttd = new List <AttdLog>(); foreach (var line in lines) { //O;2018-12-08 05:54:06;20007766;192.168.6.100;0 string[] trow = line.Split(';'); if (trow.Length >= 4) { AttdLog t = new AttdLog(); t.AddID = "upload"; t.AddDt = new DateTime(2018, 12, 12); t.EmpUnqID = trow[2].ToString(); t.IOFLG = trow[0].ToString(); t.LunchFlg = (trow[4].ToString() == "0" ? false : true); t.MachineIP = trow[3].ToString(); t.PunchDate = DateTime.ParseExact(trow[1].ToString(), "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture); t.t1Date = t.PunchDate.Date; t.TableName = Utils.Helper.GetAttdTableName(t.MachineIP, Utils.Helper.constr); t.tYear = t.PunchDate.Year; t.tYearMt = Convert.ToInt32(t.PunchDate.ToString("yyyyMM")); listAttd.Add(t); } } foreach (AttdLog t in listAttd) { string dberr = Utils.Helper.AttdLogStoreToDb(t); string err = string.Empty; if (!string.IsNullOrEmpty(dberr)) { t.Error = dberr; err = "Error while store to db : " + t.EmpUnqID + " : " + dberr + Environment.NewLine; } if (!string.IsNullOrEmpty(err)) { string newFileName = Path.Combine(Path.GetDirectoryName(tpath), "Processed", "Errors.txt"); using (System.IO.StreamWriter file = new System.IO.StreamWriter(newFileName, true)) { file.WriteLine(tpath + "->" + err); } } } ListViewItem row = new ListViewItem(tpath); row.SubItems.Add(new ListViewItem.ListViewSubItem(row, listAttd.Count.ToString())); listView1.Items.Add(row); listView1.Refresh(); Application.DoEvents(); } }