删除行-sqlc#

64jmpszr  于 2021-06-25  发布在  Mysql
关注(0)|答案(2)|浏览(192)

你好,我想从我的表中删除一行,我有3个表用于配方,配料,我正在使用第3个表来组合配方和配料表,
我的问题我可以添加一行,但我不知道如何删除一行我真的不知道怎么做。
“代码关闭plz帮助^ ^”

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();
    }
kupeojn6

kupeojn61#

这是:

INSERT INTO RecpieIngredient VALUES (@RecpieId,@IngredientId)

是指:

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

这个 DELETE 语句没有这样的速记变体,它要求您明确地提到列名:

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

不过,你自己几乎解决了这个问题:)

eni9jsuy

eni9jsuy2#

你删除的语句是错误的。应该是的

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

相关问题