public void OpenAndClose() { CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt"); Assert.AreEqual(reader.IsClosed, false); reader.Close(); Assert.AreEqual(reader.IsClosed, true); reader.Dispose(); }
public void ReadAllRows() { CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt"); while (reader.Read()) { } reader.Close(); reader.Dispose(); }
public void MoveFileUsing() { using (CsvDataReader reader = new CsvDataReader(@"..\..\MoveCsv.txt")) { Assert.AreEqual(reader.IsClosed, false); reader.Close(); Assert.AreEqual(reader.IsClosed, true); } File.Move(@"..\..\MoveCsv.txt", @"..\..\MoveCsvTemp.txt"); File.Move(@"..\..\MoveCsvTemp.txt", @"..\..\MoveCsv.txt"); }
public void AddStaticValue() { Dictionary <String, String> staticColumns = new Dictionary <String, String>(); staticColumns.Add("Column1", "Value"); CsvDataReader reader = new CsvDataReader(@"..\..\SimpleCsv.txt", staticColumns); Assert.AreEqual(3, reader.GetOrdinal("Column1")); while (reader.Read()) { Assert.AreEqual("Value", reader.GetValue(reader.GetOrdinal("Column1"))); } reader.Close(); reader.Dispose(); }
public override DataTable ToDataTable(DataTableConvertParams context, Stream stream) { if (context == null) { context = new DataTableConvertParams(); } StreamReader sr = new StreamReader(stream); DataTable dt = new DataTable(); if (context.RecordDelim != '\n') { #region If Record Delimiter is overriden, itterate through all characters and replace them with new line //https://stackoverflow.com/questions/1232443/writing-to-then-reading-from-a-memorystream MemoryStream ms = new MemoryStream(); StreamWriter sw = new StreamWriter(ms); while (sr.Peek() >= 0) { Char c = (Char)sr.Read(); if (c == context.RecordDelim) { sw.Write('\n'); } else { sw.Write(c); } } sw.Flush(); ms.Position = 0; sr = new StreamReader(ms, Encoding.UTF8); #endregion If Record Delimiter is overriden, itterate through all characters and replace them with new line } //_conf.BadDataFound = cxt => //{ //For debugging (put breakpoints here) //}; _conf.Delimiter = context.FieldDelim.ToString(); if (_conf.Delimiter != ",") { _conf.IgnoreQuotes = true; } _conf.HasHeaderRecord = context.HasHeaders; _conf.MissingFieldFound = null; CsvReader csvReader = new CsvReader(sr, _conf); CsvDataReader dataReader = new CsvDataReader(csvReader); if (!context.HasHeaders) { #region If No Headers loop through all records and add columns as columns are found while (dataReader.Read()) { while (dt.Columns.Count < dataReader.FieldCount) { dt.Columns.Add($"Col{dt.Columns.Count}"); } DataRow row = dt.NewRow(); for (int i = 0; i < dataReader.FieldCount; i++) { row[i] = dataReader.GetValue(i); } dt.Rows.Add(row); } dataReader.Close(); sr.Close(); #endregion If No Headers loop through all records and add columns as columns are found } else { #region If there are headers DataTable.Load will suffice try { dt.Load(dataReader, LoadOption.Upsert); } catch (Exception ex) { throw ex; } finally { dataReader.Close(); sr.Close(); } #endregion If there are headers DataTable.Load will suffice } //CsvHelper seems to make DataTable Columns readonly foreach (DataColumn dc in dt.Columns) { dc.ReadOnly = false; } return(dt); }