The CSVHelper .NET library seems fantastic so far, but the documentation is a little lacking for a pseudo-beginner like myself.
I need to read a csv file and write the results to our SQL Server database. For the table I'm writing to, I need to map to its columns from the CSV columns, including some concatenation of multiple fields to one.
This is what I have for reading the csv file:
public static void Main(string[] args)
{
using (var reader = new StreamReader(@"C:\Users\me\Documents\file.csv"))
using (var csv = new CsvReader(reader))
{
csv.Configuration.PrepareHeaderForMatch = (string header, int index) =>
header.Replace(" ", "_").Replace("(", "").Replace(")", "").Replace(".", "");
var records = csv.GetRecords<EntityCsv>().ToList();
}
}
My EntityCsv class contains property names for all columns of the csv file.
Then, I also have a class called TaskEntity which contains the property names and types for the destination database table (although I'm unclear as to whether I need this).
Finally, per advice from a colleague, I have a method set up to make use of SQLBulkCopy as thus:
public void AddBulk(List<TaskEntity> entities)
{
using (var con = GetConnection())
{
SqlBulkCopy bulk = new SqlBulkCopy(con);
bulk.BatchSize = 2000;
bulk.BulkCopyTimeout = 0;
bulk.DestinationTableName = "dbo.CsvExports";
bulk.WriteToServer(entities.AsDataTable());
bulk.Close();
}
}
I borrowed that code block from him and would theoretically run that method as the final step.
But I know I'm missing a step in between, and that is mapping the fields from the csv to the SQL server field. I'm scratching my head at how to implement this step.
So let's say for simplicity's sake I have 3 columns in the csv file, and I want to map them to 2 columns of the SQL table as follows:
CsvColumn1 -> SQLtableColumn1
CsvColumn2 + CsvColumn3 -> SQLtableColumn2
How would I go about accomplishing this with CsvReader and C#? I have explored the Mapping section of the CSVReader documentation but everything I'm seeing in there seems to refer to mapping column names from an input file to names in an output file. I don't see anything there (nor anywhere on the Google) that speaks specifically to taking the input file and exporting its rows to a SQL database.