Created
March 14, 2018 17:19
-
-
Save vector623/63a3c4e5feb6e322b58cb5c2ac94d143 to your computer and use it in GitHub Desktop.
Example use of CsvHelper nuget package; also example usage of dynamic and RouteValueDictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void ImportPricingFile() | |
{ | |
using (var streamReader = new StreamReader(@"C:\Users\d.gallmeier\gits\daytoday\20180308\pricingFile.csv")) | |
using (var csv = new CsvReader(streamReader)) | |
using (var db = new SqlConnection(Config.AthenaConnectionString)) | |
{ | |
var pricingLines = csv | |
.GetRecords<dynamic>() | |
.Select(record => | |
{ | |
var keyValueDictionary = new RouteValueDictionary(record); | |
var pricingEntry = new ProductMargin | |
{ | |
itemId = Convert.ToInt64(Convert.ToInt64(keyValueDictionary["ItemID"]).ToString()), | |
manufacturer = keyValueDictionary["Manufacturer"].ToString(), | |
category = keyValueDictionary["Category"].ToString(), | |
d2cMargin = Convert.ToDecimal(keyValueDictionary["D2C margin %"].ToString()), | |
}; | |
return pricingEntry; | |
}) | |
.ToList(); | |
pricingLines.BulkCopyToDatabase("ProductMargins", db.ConnectionString, "dbo"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment