Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue importing CSV file using FileHelpers
    text
    copied!<p>I'm using FileHelpers library to import csv files into the database table. I'm having an issue importing the file that has the field that starts with the number ('800NUMBER') in the file header.</p> <p><strong>Code to import:</strong><br/> <code>DataTable data = CommonEngine.CsvToDataTable(file, ',');</code></p> <p><strong>Exception:</strong><br/> <code>FileHelpers.FileHelpersException: The string '800NUMBER' not is a valid .NET identifier. at FileHelpers.RunTime.FieldBuilder..ctor(String fieldName, String fieldType) at FileHelpers.RunTime.DelimitedClassBuilder.AddField(String fieldName, String fieldType) at FileHelpers.RunTime.CsvClassBuilder.AddField(String fieldName, String fieldType) at FileHelpers.RunTime.DelimitedClassBuilder.AddField(String fieldName) at FileHelpers.RunTime.CsvClassBuilder..ctor(CsvOptions options) at FileHelpers.CsvEngine.CsvToDataTable(String filename, String classname, Char delimiter, Boolean hasHeader) at FileHelpers.CommonEngine.CsvToDataTable(String filename, Char delimiter)</code></p> <p>I'm not sure if there is a way to escape the column name like '[800NUMBER]'. </p> <p>The column name cannot be changed because that's the way the client supplies it to us.</p> <p>Thank you,<br/> -Oleg</p> <hr> <p>Solved this issue by reading 'header' row separately from the 'data' rows. Then, I change the column names in the 'data' and use SqlBulkCopy to import into the database. </p> <pre><code> FileHelpers.CsvOptions options = new FileHelpers.CsvOptions("ImportRecord", ',', file); options.HeaderLines = 0; FileHelpers.CsvEngine engine = new FileHelpers.CsvEngine(options); engine.Options.IgnoreFirstLines = 0; DataTable header = engine.ReadStringAsDT(FileHelpers.CommonEngine.RawReadFirstLines(file, 1)); engine.Options.IgnoreFirstLines = 1; DataTable data = engine.ReadFileAsDT(file); for (int i = 0; i &lt; header.Columns.Count; i++) data.Columns[i].ColumnName = header.Rows[0][i].ToString(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload