/// <summary> /// This method converts one line of the CSV file to a SalesModel /// </summary> /// <param name="csvString"></param> /// <returns>SalesModel object</returns> /// <remarks>This is where you would do any data formatting.</remarks> private SalesModel GetModelFromString(string csvString) { try { if (!string.IsNullOrWhiteSpace(csvString)) { string[] values = csvString.Split(','); SalesModel model = new SalesModel(); model.OrderId = values[6].AsLong(); model.Country = values[1]; model.ItemType = values[2]; model.OrderDate = values[5].AsDateTime(); model.OrderPriority = values[4]; model.Region = values[0]; model.SalesChannel = values[3]; model.ShipDate = values[7].AsDateTime(); model.TotalCost = values[12].AsDouble(); model.TotalProfit = values[13].AsDouble(); model.TotalRevenue = values[11].AsDouble(); model.UnitCost = values[10].AsDouble(); model.UnitPrice = values[9].AsDouble(); model.UnitsSold = values[8].AsLong(); return(model); } return(null); } catch (Exception) { throw; } }
/// <summary> /// This method retrieves product data from a CSV file and fills the product list. /// </summary> /// <remarks>Normally this would be a database call but a CSV file is easier to deploy for a demo application.</remarks> private void GetSalesData() { SalesList = new List <SalesModel>(); //read the CSV file into an array of lines string[] lines = File.ReadAllLines("Sales.csv", Encoding.Latin1); //for each line convert the data into a model object for (int x = 1; x < lines.Length; x++) { SalesModel model = GetModelFromString(lines[x]); if (model != null) { //add the model object to the list SalesList.Add(model); } } //sort the list by the id SalesList = SalesList.OrderBy(x => x.OrderId).ToList(); }
public int InsertSalesData(SalesModel model) { Dictionary <string, object> parameters = model.ConvertModelToDictionary(); return(ExecuteNonQuery(_connectionString, "InsertSalesData", parameters)); }