如何在 C# 中获取导致 SqlException 的实际 SQL?

发布于 2024-10-14 23:59:03 字数 1278 浏览 4 评论 0原文

可能的重复:
获取导致 SQLException 的 Query/CommandText

我正在工作在某些错误处理代码(使用 elmah)上,默认设置仅发送错误消息。我想知道引发错误的实际 SQL(即“SELECT * FROM thisTableDoesNotExist”)

这是我到目前为止所拥有的:

if (e.Error.Exception is SqlException)
{
    //if SQL exception try to give some extra information
    SqlException sqlEx = e.Error.Exception as SqlException;
    e.Mail.Body = e.Mail.Body + "<div>" +
                                "<h1>SQL EXCEPTION</h1>" +
                                "<b>Message</b>: " + sqlEx.Message +
                                "<br/><b>LineNumber:</b> " + sqlEx.LineNumber + 
                                "<br/><b>Source:</b> " + sqlEx.Source +
                                "<br/><b>Procedure:</b> " + sqlEx.Procedure +
                                "</div>";
}

而且我希望能够显示实际的 SQL。数据库是 SQL Server 2008,SqlException 的类型为 System。 Data.SqlClient.SqlException

Possible Duplicate:
Obtain the Query/CommandText that caused a SQLException

I am working on some error handling code (using elmah) and the default setup only sends the error message. I would like to know the actual SQL that throws an error (i.e. "SELECT * FROM thisTableDoesNotExist")

This is what I have so far:

if (e.Error.Exception is SqlException)
{
    //if SQL exception try to give some extra information
    SqlException sqlEx = e.Error.Exception as SqlException;
    e.Mail.Body = e.Mail.Body + "<div>" +
                                "<h1>SQL EXCEPTION</h1>" +
                                "<b>Message</b>: " + sqlEx.Message +
                                "<br/><b>LineNumber:</b> " + sqlEx.LineNumber + 
                                "<br/><b>Source:</b> " + sqlEx.Source +
                                "<br/><b>Procedure:</b> " + sqlEx.Procedure +
                                "</div>";
}

And I would like to be able to also show the actual SQL. The database is SQL Server 2008 and SqlException is of type System.Data.SqlClient.SqlException.

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

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

发布评论

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

评论(4

梦冥 2024-10-21 23:59:03

不可能。您需要捕获执行 SQL 命令的异常,然后将命令文本包含在您自己的自定义异常中。请参阅获取导致 SQLException 的查询/CommandText

Not possible. You'll need to catch the exception where the SQL command was executed, and then include your command text in your own custom exception. See Obtain the Query/CommandText that caused a SQLException.

云朵有点甜 2024-10-21 23:59:03

您可以在 SQL 中包含错误处理代码,当遇到错误时,您可以使用 print 或 returns 语句发回它尝试运行的 SQL,或者以您希望的方式将其返回到应用程序。

You could have error handling code in your SQL, and when it encounters an error, you can send back the SQL that it attempted to run with a print or returns statement or however you want to return it to your application.

又怨 2024-10-21 23:59:03

检查异常的最佳方法是在发生异常的代码中放置一个断点,并检查异常对象图的值。

尝试 InnerException 的 Message 成员,如下所示

sqlEx.InnerException.Message

:可能无法提供失败的确切 SQL,但可能会为您提供更具体的信息,例如操作和表名称。 StackTrace 成员也可能有一些信息。

The best way to examine the exception is to put a breakpoint in your code where the exception happens and examine the values of the exception object graph.

Try the Message member of the InnerException like this:

sqlEx.InnerException.Message

It may not provide the exact SQL that failed but may give you more specific information such as the operation and the table name. The StackTrace member may also have some information.

飘然心甜 2024-10-21 23:59:03

如果您无法在 C# 方面获得足够的信息,您可以使用“SQL Profiler”(完整 MS SQL 的一部分)来查看执行了哪些命令。

有关 SQL 事件探查器的信息 http://msdn.microsoft.com/en-us/library /ms181091.aspx 。如果您没有分析器,您还应该能够使用底层跟踪 API - http ://msdn.microsoft.com/en-us/library/ms191006.aspx

If you can't get enough information on C# side you can use "SQL profiler" (part of complete MS SQL) to see what commands where executed.

Information on SQL Profiler http://msdn.microsoft.com/en-us/library/ms181091.aspx . You should also be able to use underlying Tracing API if you don't have profiler - http://msdn.microsoft.com/en-us/library/ms191006.aspx

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