public static void Remove(ProductItem ProductItem) { //更新資料庫 ProductItemAccessor.Delete(ProductItem); //更新記憶体 ProductItemCache.Remove(ProductItem); }
internal static void UpdateInsert(ProductItem ProductItem) { using (SqlConnection SqlConnection = ConnectionManager.GetConnection()) { using (SqlCommand SqlCommand = SqlConnection.CreateCommand()) { SqlCommand.CommandText = "UPDATE " + " Product_ProductItem " + "SET " + " Name = @Name " + " ,Tags = @Tags " + " ,OriginalPrice = @OriginalPrice " + " ,Discount = @Discount " + " ,SalePrice = @SalePrice " + " ,Inventory = @Inventory " + " ,Sort = @Sort " + " ,UpdateTime = @UpdateTime " + " ,CreateTime = @CreateTime " + "WHERE " + " Id = @Id " + "IF @@ROWCOUNT = 0 " + "BEGIN " + "INSERT INTO " + " Product_ProductItem " + "( Id, Name, Tags, OriginalPrice, Discount, SalePrice, Inventory, Sort, UpdateTime, CreateTime ) " + "VALUES " + "( @Id, @Name, @Tags, @OriginalPrice, @Discount, @SalePrice, @Inventory, @Sort, @UpdateTime, @CreateTime ) " + "END "; SqlCommand.Parameters.AddWithValue("Id", ProductItem.Id); SqlCommand.Parameters.AddWithValue("Name", ProductItem.Name); SqlCommand.Parameters.AddWithValue("Tags", string.Join(",", ProductItem.Tags.Select(t => t.Replace(",", "�A")))); SqlCommand.Parameters.AddWithValue("OriginalPrice", ProductItem.OriginalPrice); SqlCommand.Parameters.AddWithValue("Discount", ProductItem.Discount); SqlCommand.Parameters.AddWithValue("SalePrice", ProductItem.SalePrice); SqlCommand.Parameters.AddWithValue("Inventory", ProductItem.Inventory); SqlCommand.Parameters.AddWithValue("Sort", ProductItem.Sort); SqlCommand.Parameters.AddWithValue("UpdateTime", ProductItem.UpdateTime); SqlCommand.Parameters.AddWithValue("CreateTime", ProductItem.CreateTime); SqlConnection.Open(); SqlCommand.ExecuteNonQuery(); } } }
internal static void Delete(ProductItem ProductItem) { using (SqlConnection SqlConnection = ConnectionManager.GetConnection()) { using (SqlCommand SqlCommand = SqlConnection.CreateCommand()) { SqlCommand.CommandText = "Delete " + " Product_ProductItem " + "WHERE " + " Id = @Id "; SqlCommand.Parameters.AddWithValue("Id", ProductItem.Id); SqlConnection.Open(); SqlCommand.ExecuteNonQuery(); } } }
public static void Save(ProductItem ProductItem) { Save(new List<ProductItem>() { ProductItem }); }
private static List<ProductItem> Get(SqlCommand SqlCommand) { List<ProductItem> ProductItems = new List<ProductItem>(); using (SqlConnection SqlConnection = ConnectionManager.GetConnection()) { using (SqlCommand.Connection = SqlConnection) { SqlConnection.Open(); SqlDataReader SqlDataReader = SqlCommand.ExecuteReader(); while (SqlDataReader.Read()) { string Id = (string)SqlDataReader["Id"]; string Name = (string)SqlDataReader["Name"]; List<string> Tags = SqlDataReader["Tags"].ToString().Split(',').ToList(); string OriginalPrice = (string)SqlDataReader["OriginalPrice"]; string Discount = (string)SqlDataReader["Discount"]; decimal SalePrice = (decimal)SqlDataReader["SalePrice"]; int Inventory = (int)SqlDataReader["Inventory"]; int Sort = (int)SqlDataReader["Sort"]; DateTime UpdateTime = (DateTime)SqlDataReader["UpdateTime"]; DateTime CreateTime = (DateTime)SqlDataReader["CreateTime"]; ProductItem ProductItem = new ProductItem(Id, Name, Tags, OriginalPrice, Discount, SalePrice, Inventory, Sort, UpdateTime, CreateTime); ProductItems.Add(ProductItem); } } } return ProductItems; }