为什么 SqlException 没有被抛出/捕获?

发布于 2025-01-17 08:07:20 字数 1038 浏览 5 评论 0原文

我在库中有以下代码来对数据库记录执行删除操作。我的代码使用 Dapper 库。当我调用 Dapper Execute() 方法时发生错误。

某些数据库表记录无法删除,因为它们被其他表中的相关记录引用。当无法删除记录时,会发生数据库错误“DELETE 语句与 REFERENCE 约束冲突”(如预期)。调用 Execute 时,此错误会导致 System.Data.SqlClient.SqlException,并且此异常会导致我的程序崩溃。

我写了一个try/catch来处理这个错误,但是程序仍然在try中崩溃并且没有移动到catch块。看来 Execute() 可能不会抛出异常?我也尝试使用 Query() 方法获得相同的结果。我不知道如何捕捉和处理这个异常!

private IDbConnection db;

public Repository(string connectionString)
{
    db = new SqlConnection(connectionString);
}

public void DeletePerson(string PersonID)
{
    string query = "DELETE FROM PERSON WHERE PersonID = @PersonID";

    try
    {
        db.Execute(query, new { PersonID });  //program crashes here b/c of SqlException
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

这是调用此方法的代码(来自不同的项目)。它也在 try/catch 中,但永远不会到达 catch。

string id = "BELWIT";
try
{
    conn.DeletePerson(id);
}
catch
{
    MessageDialog errorMsg = new MessageDialog("Delete failed.");
    errorMsg.ShowAsync();
}

I have the following code in a library to perform a delete operation on database records. My code uses the Dapper library. The error is happening when I call the Dapper Execute() method.

Some of the database table records can not be deleted because they are referenced by related records in other tables. When a record can't be deleted, the database error "The DELETE statement conflicted with the REFERENCE constraint" happens (as expected). This error causes a System.Data.SqlClient.SqlException when calling Execute, and this exception crashes my program.

I wrote a try/catch to handle this error, but the program still crashes in the try and doesn't move to the catch block. It seems that maybe Execute() doesn't throw the exception? I've also tried using the Query() method with the same outcome. I am at a loss as to how I can catch and deal with this exception!

private IDbConnection db;

public Repository(string connectionString)
{
    db = new SqlConnection(connectionString);
}

public void DeletePerson(string PersonID)
{
    string query = "DELETE FROM PERSON WHERE PersonID = @PersonID";

    try
    {
        db.Execute(query, new { PersonID });  //program crashes here b/c of SqlException
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Here is the code that calls this method (from a different project). It is also in a try/catch, but the catch is never reached.

string id = "BELWIT";
try
{
    conn.DeletePerson(id);
}
catch
{
    MessageDialog errorMsg = new MessageDialog("Delete failed.");
    errorMsg.ShowAsync();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2025-01-24 08:07:20

看起来这个错误实际上是由我的 Visual Studio 设置引起的。

异常设置中有一个复选框,其中为 System.Data.SqlClient.SqlException 选中了“抛出时中断”。所以它破坏并停止了 Visual Studio 中的执行。忽略异常弹出窗口并单击“继续”使我的代码可以继续执行 catch 块。

愚蠢的结果并考虑删除,但也许将其留在这里以防其他人遇到同样的问题可能会有所帮助?

It looks like this error was actually caused by my Visual Studio settings.

There was a checkbox in Exception Settings where 'Break When Thrown' was checked for System.Data.SqlClient.SqlException. So it was breaking and stopping execution in Visual Studio. Ignoring the exception popup and clicking 'Continue' allowed my code to move on to the catch block.

Dumb outcome and considered deleting, but maybe leaving it here in case anyone else experiences the same issue could be helpful?

野稚 2025-01-24 08:07:20

看起来 errorMsg.ShowAsync() 是一个异步方法。一种可能性是您没有等待该方法,因此程序在显示该消息之前完成执行。您可以尝试将其更改为

await errorMsg.ShowAsync();

您还需要将调用该方法的任何方法标记为异步。

It seems like errorMsg.ShowAsync() is an async method. One possibility is that you are not awaiting that method, so the program finishes execution before it can show that message. Can you try changing it to

await errorMsg.ShowAsync();

You'll also need to mark whatever method is calling that as async.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文