简体   繁体   English

使用 SqlCommand 从多个表中删除

[英]Delete from multiple tables with SqlCommand

When I try to delete a whole row from 2 tables with my INNER JOIN I get the error shown at the bottom.当我尝试使用 INNER JOIN 从 2 个表中删除一整行时,出现底部显示的错误。 I have searched on the internet and could not find the problem so I came here for help.我在互联网上搜索并找不到问题,所以我来这里寻求帮助。

Here is the code:这是代码:

    var delete = new SqlCommand("DELETE Posts, Comments FROM Posts INNER JOIN Comments ON Posts.PostId = Comments.PostId WHERE Posts.PostId = @PostId;");
    delete.Parameters.AddWithValue("@PostId", postId);
    _dataAccess.ExecuteQuery(delete);

I am getting an error message:我收到一条错误消息:

System.Data.SqlClient.SqlException: 'Incorrect syntax near ','.'

Your Problem in the SQL Statement , it is not valid.您在SQL Statement中的问题,它是无效的。

You should divide the statement into two ones:您应该将声明分为两个:

First Delete Comments , then delete Posts先删除Comments ,再删除Posts

Sequence is Important顺序很重要

var deleteComments = new SqlCommand("DELETE Comments FROM Posts INNER JOIN Comments ON Posts.PostId = Comments.PostId WHERE Posts.PostId = @PostId;");
        deleteComments.Parameters.AddWithValue("@PostId", postId);
        _dataAccess.ExecuteQuery(deleteComments);
        
var deletePosts = new SqlCommand("DELETE Posts WHERE PostId= @PostId;");
        deletePosts.Parameters.AddWithValue("@PostId", postId);
        _dataAccess.ExecuteQuery(deletePosts);

The other option, using one statement:另一种选择,使用一个语句:

var delete = new SqlCommand("DELETE Comments FROM Posts INNER JOIN Comments ON Posts.PostId = Comments.PostId WHERE Posts.PostId = @PostId; DELETE Posts WHERE PostId= @PostId;");
            delete.Parameters.AddWithValue("@PostId", postId);
            _dataAccess.ExecuteQuery(delete);

More Explanation:更多解释:

Using the Following Prepared SQL Script using SQL Studio (SSMS):使用SQL Studio (SSMS) 使用以下准备好的 SQL 脚本:

CREATE TABLE Posts (PostId  INT, PostText varchar(20))
CREATE TABLE Comments (CommentId INT, PostId INT, CommentText varchar(20))

INSERT INTO Posts VALUES (1, 'text')
INSERT INTO Comments VALUES (1,1, 'comment here')

when I run your DELETE statement当我运行你的 DELETE 语句时

DELETE Posts, Comments FROM Posts INNER JOIN Comments ON Posts.PostId = Comments.PostId WHERE Posts.PostId = 1

It gives me the same error它给了我同样的错误

When I run当我跑

DELETE Comments FROM Posts INNER JOIN Comments ON Posts.PostId = Comments.PostId WHERE Posts.PostId = 1;

DELETE Posts WHERE PostId = 1;

It works fine.它工作正常。

So the rule of thumb in such cases is to use SSMS (MS SQL Studio) to test your SQL statement first and then implement it in C#.因此,在这种情况下,经验法则是先使用SSMS (MS SQL Studio)测试您的 SQL 语句,然后在 C# 中实现它。

There is no option in MSSQL Server to remove data from multiple tables in one statement. MSSQL Server中没有选项可以在一个语句中从多个表中删除数据。 So, your statement DELETE Posts, Comments FROM is incorrect.因此,您的声明DELETE Posts, Comments FROM是不正确的。

You have two options to set this foreign key to be CASCADE DELETE or to rewrite your statement for deleting data in these two tables like the following one:您有两个选项可以将此外键设置为CASCADE DELETE或重写您的语句以删除这两个表中的数据,如下所示:

var delete = new SqlCommand("DELETE FROM Comments WHERE PostId = @PostId; 
                             DELETE FROM Posts WHERE PostId = @PostId;");
delete.Parameters.AddWithValue("@PostId", postId);
_dataAccess.ExecuteQuery(delete);

Your MS SQL query is incorrect.您的 MS SQL 查询不正确。 If you want to delete from mutiple tables, you have two choices:如果要从多个表中删除,您有两种选择:

  1. setup cascading delete where a delete in one table will automatically cause deletes in dependent tables设置级联删除,其中一个表中的删除将自动导致从属表中的删除

  2. write separate delete queries such as:编写单独的删除查询,例如:

  • DELETE FROM Comments WHERE PostId = @PostId从评论中删除 PostId = @PostId
  • DELETE FROM Posts WHERE PostId = @PostId从帖子中删除 PostId = @PostId

However, I suggest that you test your queries through the SQL Management Studio first before trying to develop code for them.但是,我建议您先通过 SQL Management Studio 测试您的查询,然后再尝试为其开发代码。

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

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