public int SaveWithSql(entities.Product product) { entities.Product hasProduct = FindOne(product.GetId()); SQLiteCommand command; if (product.GetId() == 0 | hasProduct == null) { Add(product); command = new SQLiteCommand("INSERT INTO product(sku, name, price, category_id, image, amount) VALUES(@sku, @name, @price, @category_id, @image, @amount)", db.connection); } else { command = new SQLiteCommand("UPDATE product SET sku = @sku, name = @name, price = @price, category_id = @category_id, image = @image, amount = @amount WHERE id = @id ", db.connection); } command.Parameters.AddWithValue("@id", product.GetId()); command.Parameters.AddWithValue("@sku", product.GetSku()); command.Parameters.AddWithValue("@name", product.GetName()); command.Parameters.AddWithValue("@price", Convert.ToDecimal(product.GetPrice())); command.Parameters.AddWithValue("@category_id", product.GetCategoryId()); command.Parameters.AddWithValue("@image", product.GetImage()); command.Parameters.AddWithValue("@amount", product.GetAmount()); int num = command.ExecuteNonQuery(); return(num); }
public Tuple <int, int, int> LoadProducts(repositories.Product products, repositories.Category categories) { List <string[]> lines = ParseCsv(GetCsvString("products")); int added = 0, changeprice = 0, changeamount = 0; foreach (string[] line in lines) { int id = Convert.ToInt32(line[0]); string sku = line[1]; string name = line[2]; double price; if (line[3] == "") { price = 0; } else { price = Convert.ToDouble(line[3]); } int category_id; if (line[4] != "") { category_id = Convert.ToInt32(line[4]); } else { category_id = 0; } string image = line[5]; int amount; if (line[6] != "") { amount = Convert.ToInt32(line[6]); } else { amount = 0; } entities.Product product = products.FindOne(id); if (product == null) { dynamic category; if (category_id != 0) { category = categories.FindOne(category_id); } else { category = null; } entities.Product addProduct = new entities.Product(id, sku, name, price, category, image, amount); products.SaveWithSql(addProduct); added++; } else { if (product.GetPrice() != price) { changeprice++; } if (product.GetAmount() != amount) { changeamount++; } product.SetName(name); product.SetSku(sku); product.SetCategoryId(category_id); product.SetPrice(price); product.SetImage(image); product.SetAmount(amount); products.SaveWithSql(product); } } return(new Tuple <int, int, int>(added, changeprice, changeamount)); }