public void WriteFileNotifyPercent() { actual = 0; actualAdd = 25; engine = new FileHelperEngine(typeof(SampleType)); engine.SetProgressHandler(new ProgressChangeHandler(ProgressChange), ProgressMode.NotifyPercent); SampleType[] res = new SampleType[2]; res[0] = new SampleType(); res[1] = new SampleType(); res[0].Field1 = DateTime.Now.AddDays(1); res[0].Field2 = "je"; res[0].Field3 = 0; res[1].Field1 = DateTime.Now; res[1].Field2 = "ho"; res[1].Field3 = 2; engine.WriteFile("prog1.txt", res); if (File.Exists("prog1.txt")) { File.Delete("prog1.txt"); } }
private void cmdRun_Click(object sender, EventArgs e) { cmdRun.Enabled = false; FileHelperEngine engine = new FileHelperEngine(typeof(CustomersVerticalBar)); object[] records = engine.ReadFile("Customers.txt"); Application.DoEvents(); engine.SetProgressHandler(new ProgressChangeHandler(ProgressChange)); engine.WriteString(records); cmdRun.Enabled = true; }
public void ReadFileNotifyRecord() { actual = 0; actualAdd = 1; engine = new FileHelperEngine(typeof(SampleType)); engine.SetProgressHandler(new ProgressChangeHandler(ProgressChange)); SampleType[] res; res = (SampleType[])TestCommon.ReadTest(engine, @"Good\test1.txt"); Assert.AreEqual(4, res.Length); Assert.AreEqual(4, engine.TotalRecords); Assert.AreEqual(0, engine.ErrorManager.ErrorCount); }
private void cmdRun_Click(object sender, EventArgs e) { cmdRun.Enabled = false; FileHelperEngine engine = new FileHelperEngine(typeof (CustomersVerticalBar)); object[] records = engine.ReadFile(MainClass.GlobalTestFile); Application.DoEvents(); engine.SetProgressHandler(new ProgressChangeHandler(ProgressChange)); engine.WriteString(records); cmdRun.Enabled = true; }
private csvImport[] ReadCsv(string file, BackgroundWorker bw, bool unattended) { try { FileHelperEngine engine = new FileHelperEngine(typeof(csvImport)); engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; engine.SetProgressHandler(new ProgressChangeHandler(ReadProgressCSV)); var resCSV = engine.ReadFile(file) as csvImport[]; if (engine.ErrorManager.HasErrors) { List<string> filtLines = new List<string> () {}; foreach (ErrorInfo err in engine.ErrorManager.Errors) { if (RemoveExtraDelimiter(err.RecordString, filtLines)) { Log.d("Import: Gjenopprettet linje " + err.LineNumber + " med feil format."); } else { importReadErrors++; Log.n("Import: Format feil ved linje " + err.LineNumber + ": " + err.RecordString, Color.Red); Log.d("Import: Feilmelding: " + err.ExceptionInfo.ToString()); if (importReadErrors > 100) { Log.n("CSV er skadet eller inneholder ikke riktige ranking transaksjoner.", Color.Red); if (!unattended) { FormError errorMsg = new FormError("CSV er skadet eller inneholder ikke riktige ranking transaksjoner.", err.ExceptionInfo, "Transaksjoner som ikke kunne leses:\n" + filtLines.Count); errorMsg.ShowDialog(); } return null; } } } if (filtLines.Count > 0) { Log.d("Import: Forsøker å legge til " + filtLines.Count + " linjer med feil på nytt etter fix..."); Log.d("Import: resCSV length før: " + resCSV.Length); string tmpFile = GetTempFilename(".txt"); File.WriteAllLines(tmpFile, filtLines.ToArray(), Encoding.Unicode); var resCSVfixed = engine.ReadFile(tmpFile) as csvImport[]; int oldLength = resCSV.Length; Array.Resize<csvImport>(ref resCSV, oldLength + resCSVfixed.Length); Array.Copy(resCSVfixed, 0, resCSV, oldLength, resCSVfixed.Length); Log.d("Import: resCSV length etter: " + resCSV.Length); } } Log.d("Import: ReadCsv ferdig '" + file + "' - Totalt: " + resCSV.Length); return resCSV; } catch (IOException ex) { Log.n("CSV var låst for lesing. Forleng ventetid i makro hvis overføringen ikke ble ferdig i tide.", Color.Red); Log.d("CSV var låst.", ex); return null; } catch (Exception ex) { throw new Exception("Kritisk feil under lesing av CSV!", ex); } }
public bool Import(string filename, BackgroundWorker bw = null) { try { if (!File.Exists(filename)) { Log.n("Vare import: Fant ikke fil eller ble nektet tilgag. (" + filename + ")", Color.Red); return false; } int importReadErrors = 0; var engine = new FileHelperEngine(typeof(csvObsolete)); engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; main.processing.SetProgressStyle = ProgressBarStyle.Marquee; engine.SetProgressHandler(new ProgressChangeHandler(ReadProgressCSV)); var resCSV = engine.ReadFile(filename) as csvObsolete[]; if (engine.ErrorManager.HasErrors) { foreach (ErrorInfo err in engine.ErrorManager.Errors) { importReadErrors++; Log.e("Import: Klarte ikke lese linje " + err.LineNumber + ": " + err.RecordString); Log.d("Exception: " + err.ExceptionInfo.ToString()); if (importReadErrors > 100) { Log.e("Feil: CSV er ikke en obsolete eksportering eller filen er skadet. (" + filename + ")"); return false; } } } main.processing.SetProgressStyle = ProgressBarStyle.Continuous; int count = resCSV.Length; if (count == 0) return false; Log.n("Prosesserer " + count.ToString("#,##0") + " vare oppføringer.. (" + filename + ")"); DateTime dtLast = DateTime.MinValue; DataTable tableUkurans = main.database.tableUkurans.GetDataTable(); DataTable tableInfo = main.database.tableVareinfo.GetAllProducts(); if (tableInfo == null) throw new Exception("Intern feil i databasen. tableInfo var null!"); int countInfo = tableInfo.Rows.Count; Log.d("Product info table has " + string.Format("{0:n0}", countInfo) + " rows before import"); for (int i = 0; i < count; i++) { if (bw != null) { if (bw.CancellationPending) return false; if (i % 83 == 0) { bw.ReportProgress(i, new StatusProgress(count, null, 0, 100)); main.processing.SetText = "Lagrer varebeholdning..: " + string.Format("{0:n0}", i) + " / " + string.Format("{0:n0}", count); } } string varekode = resCSV[i].Varekode; if (varekode.Length > 0) varekode = varekode.Substring(1, varekode.Length - 1); DataRow[] filter = tableInfo.Select(TableVareinfo.KEY_VAREKODE + " = '" + varekode + "'"); if (filter.Length == 0) { DataRow row = tableInfo.NewRow(); row[TableVareinfo.INDEX_VAREKODE] = varekode; row[TableVareinfo.INDEX_TEKST] = resCSV[i].VareTekst; row[TableVareinfo.INDEX_KAT] = (int)resCSV[i].Kat; row[TableVareinfo.INDEX_KATNAVN] = resCSV[i].KatNavn; row[TableVareinfo.INDEX_GRUPPE] = (int)resCSV[i].Grp; row[TableVareinfo.INDEX_GRUPPENAVN] = resCSV[i].GrpNavn; row[TableVareinfo.INDEX_MODGRUPPE] = (int)resCSV[i].Mod; row[TableVareinfo.INDEX_MODGRUPPENAVN] = resCSV[i].ModNavn; row[TableVareinfo.INDEX_MERKE] = (int)resCSV[i].Merke; row[TableVareinfo.INDEX_MERKENAVN] = resCSV[i].MerkeNavn; row[TableVareinfo.INDEX_DATO] = DateTime.Now; tableInfo.Rows.Add(row); } if (resCSV[i].Avd == main.appConfig.Avdeling || resCSV[i].Avd == (main.appConfig.Avdeling + 1000)) { DataRow dtRow = tableUkurans.NewRow(); dtRow[TableUkurans.INDEX_AVDELING] = Convert.ToInt32(resCSV[i].Avd); dtRow[TableUkurans.INDEX_VAREKODE] = varekode; dtRow[TableUkurans.INDEX_ANTALL] = Convert.ToInt32(resCSV[i].AntallLager); dtRow[TableUkurans.INDEX_KOST] = Convert.ToDecimal(resCSV[i].KostVerdiLager); dtRow[TableUkurans.INDEX_DATO] = resCSV[i].DatoInnLager; dtRow[TableUkurans.INDEX_UKURANS] = Convert.ToDecimal(resCSV[i].UkuransVerdi); var s = resCSV[i].UkuransProsent; decimal d = 0; if (s.Length > 0) { s = s.Substring(0, s.Length - 1); if (s.StartsWith(",")) s = "0" + s; d = Convert.ToDecimal(s); dtRow[TableUkurans.INDEX_UKURANSPROSENT] = d; } else dtRow[TableUkurans.INDEX_UKURANSPROSENT] = 0; tableUkurans.Rows.Add(dtRow); if (DateTime.Compare(resCSV[i].DatoInnLager, dtLast) > 0) dtLast = resCSV[i].DatoInnLager; } } if (tableUkurans.Rows.Count == 0) { Log.e("Fant ikke varebeholding data for din avdeling, eller CSV inneholdte ikke wobsolete data. Forsøk igjen?"); return false; } main.processing.SetText = "Fullfører vare import.."; main.database.tableUkurans.Reset(); main.database.DoBulkCopy(tableUkurans, TableUkurans.TABLE_NAME); int countInfoAfter = tableInfo.Rows.Count; if (countInfoAfter > countInfo) { Log.d("Product info table has " + string.Format("{0:n0}", countInfo) + " rows after import. " + string.Format("{0:n0}", (countInfoAfter - countInfo)) + " new products found"); main.database.DoBulkCopy(tableInfo, TableVareinfo.TABLE_NAME); } else Log.d("No new products found. Nothing to save to product info table"); main.appConfig.dbObsoleteUpdated = dtLast; main.appConfig.dbStoreFrom = FormMain.rangeMin; main.appConfig.dbStoreTo = FormMain.rangeMin; Log.d("Forbereder lagring av historikk for lager.."); SaveHistory(MakeTableObsolete(false), dtLast, false); SaveHistory(MakeTableObsolete(true), dtLast, true); Log.d("Lagring av historikk fullført."); return true; } catch (Exception ex) { Log.ErrorDialog(ex, "Feil ved importering av lager", "KGSA Database"); } return false; }
public bool Import(string filename, FormProcessing processing, BackgroundWorker bw = null) { try { main.processing = processing; if (!File.Exists(filename)) { Log.n("Service Import: Fant ikke CSV service fil eller ble nektet tilgang. (" + filename + ")", Color.Red); return false; } var engine = new FileHelperEngine(typeof(csvService)); engine.ErrorManager.ErrorMode = ErrorMode.SaveAndContinue; processing.SetProgressStyle = ProgressBarStyle.Marquee; engine.SetProgressHandler(new ProgressChangeHandler(ReadProgressCSV)); var resCSV = engine.ReadFile(filename) as csvService[]; if (engine.ErrorManager.HasErrors) foreach (ErrorInfo err in engine.ErrorManager.Errors) { importReadErrors++; Log.n("Import: Klarte ikke lese linje " + err.LineNumber + ": " + err.RecordString, Color.Red); Log.d("Exception: " + err.ExceptionInfo.ToString()); if (importReadErrors > 100) { Log.n("Feil: CSV er ikke en service eksportering eller filen er skadet. (" + filename + ")", Color.Red); return false; } } processing.SetProgressStyle = ProgressBarStyle.Continuous; int count = resCSV.Length; if (count > 0) { DateTime dtFirst = DateTime.MaxValue; DateTime dtLast = DateTime.MinValue; string sqlStrAvd = ""; for (int i = 0; i < count; i++) { if (main.appConfig.importSetting != "FullFavoritt") if (!sqlStrAvd.Contains(resCSV[i].Avd.ToString())) // Bygg SQL streng av avdelinger som skal slettes. sqlStrAvd = sqlStrAvd + "Avdeling = '" + resCSV[i].Avd.ToString() + "' OR "; DateTime dtTemp = Convert.ToDateTime(resCSV[i].DatoMottatt.ToString()); if (DateTime.Compare(dtTemp, dtFirst) < 0) dtFirst = dtTemp; if (DateTime.Compare(dtTemp, dtLast) > 0) dtLast = dtTemp; } int dager = (dtLast - dtFirst).Days; if (dager < 60) { Log.n("Feil: CSV inneholder for kort periode. Velg minst 60 dager perioder.", Color.Red); return false; } if (dtLast < dbServiceDatoTil) { Log.n("Feil: CSV inneholder bare servicer som er eldre enn eksisterende servicer.", Color.Red); return false; } if (main.appConfig.importSetting == "FullFavoritt") foreach (string avdel in FormMain.Favoritter) sqlStrAvd = sqlStrAvd + "Avdeling = '" + avdel + "' OR "; if (sqlStrAvd.Length > 3) // Remove the last "OR" sqlStrAvd = sqlStrAvd.Remove(sqlStrAvd.Length - 3); var command = new SqlCeCommand("DELETE FROM tblService WHERE (DatoMottat >= '" + dtFirst.ToString("yyy-MM-dd") + "') AND (DatoMottat <= '" + dtLast.ToString("yyy-MM-dd") + "') AND (" + sqlStrAvd + ")", main.connection); var result = command.ExecuteNonQuery(); Log.d("Slettet " + result + " servicer."); command = new SqlCeCommand("DELETE FROM tblServiceLogg", main.connection); result = command.ExecuteNonQuery(); Log.d("Slettet " + result + " logg oppføringer."); Log.n("Prosesserer " + count.ToString("#,##0") + " service oppføringer.. (" + filename + ")"); if (main.appConfig.importSetting == "FullFavoritt") foreach (string avdel in FormMain.Favoritter) sqlStrAvd = sqlStrAvd + "Avdeling = '" + avdel + "' OR "; if (sqlStrAvd.Length > 3) // Remove the last "OR" sqlStrAvd = sqlStrAvd.Remove(sqlStrAvd.Length - 3); int ordreNr = 0; int currentRecord = 0; int currentLog = 0; int id = 0; string sql = "INSERT INTO tblService (Avdeling, Ordrenr, Navn, DatoMottat, DatoIarbeid, DatoFerdig, DatoUtlevert, Status, Selgerkode, Verksted) " + "VALUES (@Avdeling, @Ordrenr, @Navn, @DatoMottat, @DatoIarbeid, @DatoFerdig, @DatoUtlevert, @Status, @Selgerkode, @Verksted)"; for (int i = 0; i < count; i++) { if (FormMain.Favoritter.Contains(resCSV[i].Avd.ToString())) { if ((int)resCSV[i].Ordrenr != ordreNr) { ordreNr = (int)resCSV[i].Ordrenr; currentRecord++; processing.SetText = "Lagrer servicer " + currentRecord.ToString("#,##0") + ".."; if (bw != null) { if (bw.WorkerReportsProgress) bw.ReportProgress(i, new StatusProgress(count, "Prosessere servicer oppføringer..", 0, 100)); if (bw.CancellationPending) { Log.n("Service importering avbrutt av bruker!", Color.Red); return false; } } using (SqlCeCommand cmd = new SqlCeCommand(sql, main.connection)) { cmd.Parameters.AddWithValue("@Avdeling", (int)resCSV[i].Avd); cmd.Parameters.AddWithValue("@Ordrenr", (int)resCSV[i].Ordrenr); cmd.Parameters.AddWithValue("@Navn", resCSV[i].Navn); cmd.Parameters.Add("@DatoMottat", SqlDbType.DateTime).Value = Convert.ToDateTime(resCSV[i].DatoMottatt); var date = Convert.ToDateTime(resCSV[i].DatoIarbeid); if (date > FormMain.rangeMin) cmd.Parameters.Add("@DatoIarbeid", SqlDbType.DateTime).Value = date; else cmd.Parameters.Add("@DatoIarbeid", SqlDbType.DateTime).Value = FormMain.rangeMin; date = Convert.ToDateTime(resCSV[i].DatoFerdig); if (date > FormMain.rangeMin) cmd.Parameters.Add("@DatoFerdig", SqlDbType.DateTime).Value = date; else cmd.Parameters.Add("@DatoFerdig", SqlDbType.DateTime).Value = FormMain.rangeMin; date = Convert.ToDateTime(resCSV[i].DatoUtlevert); if (date > FormMain.rangeMin) cmd.Parameters.Add("@DatoUtlevert", SqlDbType.DateTime).Value = date; else cmd.Parameters.Add("@DatoUtlevert", SqlDbType.DateTime).Value = FormMain.rangeMin; cmd.Parameters.AddWithValue("@Status", resCSV[i].Status); cmd.Parameters.AddWithValue("@Selgerkode", resCSV[i].Selgerkode); cmd.Parameters.AddWithValue("@Verksted", resCSV[i].Verksted); cmd.CommandType = System.Data.CommandType.Text; cmd.ExecuteNonQuery(); cmd.CommandText = "SELECT @@IDENTITY"; id = Convert.ToInt32(cmd.ExecuteScalar()); } currentLog = 1; ImportAddLog(Convert.ToDateTime(resCSV[i].LoggDato), Convert.ToDateTime(resCSV[i].LoggTid), resCSV[i].LoggKode, resCSV[i].LoggTekst, id, currentRecord, currentLog, main.connection); } else { currentLog++; ImportAddLog(Convert.ToDateTime(resCSV[i].LoggDato), Convert.ToDateTime(resCSV[i].LoggTid), resCSV[i].LoggKode, resCSV[i].LoggTekst, id, currentRecord, currentLog, main.connection); continue; } } } } return true; } catch (IOException ex) { Log.n("CSV var låst for lesing. Forleng ventetid i makro hvis overføringen ikke ble ferdig i tide.", Color.Red); Log.Unhandled(ex); return false; } catch (Exception ex) { FormError errorMsg = new FormError("Feil ved importering av servicer", ex); errorMsg.ShowDialog(); return false; } }