0

i want to delete a product id when click a button, i only able to delete 1 table how to do with my query

enter image description here

protected void btnDelete_Click(object sender, EventArgs e)
        {
            string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection connection1 = new SqlConnection(connection);
            string sqlStatement = "DELETE FROM Product WHERE ProductID = @pid";

            try
            {
                connection1.Open();
                SqlCommand cmd = new SqlCommand(sqlStatement, connection1);
                cmd.Parameters.AddWithValue("@pid", Id);
                cmd.CommandType = CommandType.Text;
                cmd.ExecuteNonQuery();

            }
            finally
            {
                connection1.Close();
            }
        }
Mickey
  • 149
  • 1
  • 11

2 Answers2

0

Check ON DELETE CASCADE in MySql

or write more queries to first delete the records linked to your Product table.

eg.

DELETE from ProductSideImage WHERE ProductID = 1;
DELETE from ProductImage WHERE ProductID = 1;
DELETE from ProductStock WHERE ProductID = 1;
DELETE from Product WHERE ProductID = 1;

string sqlStatement = "DELETE FROM Product WHERE ProductID = @pid; DELETE FROM ProductStock WHERE ProductID = @pid; DELETE FROM ProductSideImage WHERE ProductID = @pid; DELETE FROM ProductImage WHERE ProductID = @pid;";
Community
  • 1
  • 1
Vland
  • 4,151
  • 2
  • 32
  • 43
0

Alternatively, you can do it one query, without CASCADE.

Consider the following(linking table hidden for brevity)...

 SELECT * FROM recipes;
 +-----------+-------------------------+
 | recipe_id | recipe                  |
 +-----------+-------------------------+
 |         1 | Macaroni & Cheese       |
 |         2 | Cheese on Toast         |
 |         3 | Beans on Toast          |
 |         4 | Cheese & Beans on Toast |
 |         5 | Toast & Jam             |
 |         6 | Humus                   |
 +-----------+-------------------------+

 SELECT * FROM ingredients;
 +---------------+------------+
 | ingredient_id | ingredient |
 +---------------+------------+
 |             1 | Macaroni   |
 |             2 | Cheese     |
 |             3 | Beans      |
 |             4 | Toast      |
 |             5 | Jam        |
 |             6 | Chickpeas  |
 |             7 | Tahini     |
 +---------------+------------+

 SELECT r.*
      , i.* 
   FROM recipes r 
   JOIN recipe_ingredient ri 
     ON ri.recipe_id = r.recipe_id 
   JOIN ingredients i 
     ON i.ingredient_id = ri.ingredient_id;
 +-----------+-------------------------+---------------+------------+
 | recipe_id | recipe                  | ingredient_id | ingredient |
 +-----------+-------------------------+---------------+------------+
 |         1 | Macaroni & Cheese       |             1 | Macaroni   |
 |         1 | Macaroni & Cheese       |             2 | Cheese     |
 |         2 | Cheese on Toast         |             2 | Cheese     |
 |         2 | Cheese on Toast         |             4 | Toast      |
 |         3 | Beans on Toast          |             3 | Beans      |
 |         3 | Beans on Toast          |             4 | Toast      |
 |         4 | Cheese & Beans on Toast |             2 | Cheese     |
 |         4 | Cheese & Beans on Toast |             3 | Beans      |
 |         4 | Cheese & Beans on Toast |             4 | Toast      |
 |         5 | Toast & Jam             |             4 | Toast      |
 |         5 | Toast & Jam             |             5 | Jam        |
 |         6 | Humus                   |             6 | Chickpeas  |
 |         6 | Humus                   |             7 | Tahini     |
 +-----------+-------------------------+---------------+------------+

 DELETE r
      , ri
      , i 
   FROM recipes r 
   JOIN recipe_ingredient ri 
     ON ri.recipe_id = r.recipe_id 
   JOIN ingredients i 
     ON i.ingredient_id = ri.ingredient_id 
  WHERE r.recipe = 'Humus';

 SELECT * FROM recipes;
 +-----------+-------------------------+
 | recipe_id | recipe                  |
 +-----------+-------------------------+
 |         1 | Macaroni & Cheese       |
 |         2 | Cheese on Toast         |
 |         3 | Beans on Toast          |
 |         4 | Cheese & Beans on Toast |
 |         5 | Toast & Jam             |
 +-----------+-------------------------+

 SELECT * FROM ingredients;
 +---------------+------------+
 | ingredient_id | ingredient |
 +---------------+------------+
 |             1 | Macaroni   |
 |             2 | Cheese     |
 |             3 | Beans      |
 |             4 | Toast      |
 |             5 | Jam        |
 +---------------+------------+
Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • string sqlStatement = "DELETE FROM Product,ProductStock,ProductSideImage,ProductImage WHERE ProductID = @pid"; how to link the relationship? – Mickey Dec 26 '13 at 17:36