//Read public List <BestBuyProducts> Select() { MySqlConnection conn = new MySqlConnection(); using (conn) { conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "SELECT ProductID, Name, Price " + "FROM products;"; MySqlDataReader dataReader = cmd.ExecuteReader(); List <BestBuyProducts> products = new List <BestBuyProducts>(); while (dataReader.Read()) { BestBuyProducts product = new BestBuyProducts(); product.Id = Convert.ToInt32(dataReader["ProductID"]); product.Name = dataReader["Name"].ToString(); product.Price = Convert.ToDouble(dataReader["Price"]); products.Add(product); } return(products); } }
//Create public void Insert(BestBuyProducts product) { MySqlConnection conn = new MySqlConnection(); using (conn) { MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "INSERT INTO products (Name, Price) " + "VALUES (@name, @price;"; cmd.Parameters.AddWithValue("name", product.Name); cmd.Parameters.AddWithValue("price", product.Price); cmd.ExecuteNonQuery(); } }
//Update public void Update(BestBuyProducts product) { MySqlConnection conn = new MySqlConnection(); using (conn) { conn.Open(); MySqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "UPDATE products " + "SET Name = @name, Price = @price " + "WHERE ProductID = @id;"; cmd.Parameters.AddWithValue("name", product.Name); cmd.Parameters.AddWithValue("price", product.Price); cmd.Parameters.AddWithValue("id", product.Id); cmd.ExecuteNonQuery(); } }