简体   繁体   English

删除一行-SQL C#

[英]DELETE a row - Sql C#

Hello I want to delete a row from my table , I have 3 tables for Recipe , Ingredient and i'm using the 3rd table to combine the Recipe and Ingredient tables, 您好,我想从表格中删除一行,我有3个用于Recipe,Ingredient的表格,并且我正在使用第3个表格来组合Recipe和Ingredient的表格,

my question I can add a row but I don't know how to delete a row I really dont have an idea how to do it . 我的问题是我可以添加一行,但是我不知道如何删除一行,我真的不知道该怎么做。

the code down plz help ^^" 代码下来请帮助^^“

private void btnAddToRecpie_Click(object sender, EventArgs e)
    {
        string query = "INSERT INTO RecpieIngredient VALUES (@RecpieId,@IngredientId)";

        using (connection = new SqlConnection(connectionString))
        using (SqlCommand cmnd = new SqlCommand(query, connection))
        {
            connection.Open();
            cmnd.Parameters.AddWithValue("@RecpieId", lstRecpie.SelectedValue);
            cmnd.Parameters.AddWithValue("@IngredientId",lstAllIngredient.SelectedValue);
            cmnd.ExecuteScalar();
        }
        PopulateRecpie();
    }

    private void butToDelete_Click(object sender, EventArgs e)
    {
        string query = "DELETE FROM  RecpieIngredient WHERE (@RecpieId , @IngredientId)=@RecpieId , @IngredientId ";

      //  a.Name FROM Ingredient a INNER JOIN RecpieIngredient x ON a.Id = x.IngredientId INNER JOIN Recpie m ON m.Id=x.RecpieId WHERE m.Id=" + index;
        using (connection = new SqlConnection(connectionString))
        using (SqlCommand cmnd = new SqlCommand(query, connection))
        {
            connection.Open();

            cmnd.Parameters.AddWithValue("@RecpieId", lstRecpie.SelectedValue);
            cmnd.Parameters.AddWithValue("@IngredientId", lstAllIngredient.SelectedValue);

            cmnd.ExecuteNonQuery();

        }
        PopulateRecpie();
    }

This: 这个:

INSERT INTO RecpieIngredient VALUES (@RecpieId,@IngredientId)

Is really shorthand for: 确实是:

INSERT INTO RecpieIngredient (RecpieId, IngredientId) VALUES (@RecpieId, @IngredientId)

The DELETE statement does not have such a shorthand variant, and requires you to mention the column names explicitly: DELETE语句没有这种简写形式,需要您明确提及列名:

DELETE FROM RecpieIngredient WHERE RecpieId = @RecpieId AND IngredientId = @IngredientId

You came pretty close in fixing the problem yourself though :) 不过,您自己可以很轻松地解决问题:)

You delete statement is wrong. 您删除语句是错误的。 It should be 它应该是

string query = "DELETE FROM RecpieIngredient 
                         WHERE RecpieId = @RecpieId 
                         AND IngredientId = @IngredientId";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM