如何获取详细的 ExecuteNonQuery 错误消息?

发布于 2024-11-08 17:59:51 字数 809 浏览 0 评论 0原文

我正在处理我的第一个 ASP.NET 作业。这是一个网站,我在 Visual Studio 中工作。

我不明白的是如何在 ExecuteNonQuery 失败时显示详细的错误消息。

我正在使用 OleDb 连接,所以我的猜测是我必须使用 OleDbException 或 OleDbError 来获取详细的错误消息。

基本上,问题是 - 如果 ExecuteNonQuery 失败,如何更新此代码以获得详细的错误消息?

string v1 = Request["v1"];                                    
string v2 = Request["v2"];
sql2 = "INSERT INTO table(one, two) VALUES('" + v1 + "', '" + v2  + "')";

System.Data.OleDb.OleDbConnection con2 = new System.Data.OleDb.OleDbConnection();
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data          Source=C:/Users/BB/Desktop/Database.mdb";
con2.Open();
OleDbCommand command = new OleDbCommand(sql2, con2);

try
{
    command.ExecuteNonQuery();
}
catch
{
    Response.Write("Error!");
    // detailed error message here?
}

I'm working on my first ever assignment in ASP.NET. It's a website and I work in Visual Studio.

What I can't figure out is how to get a detailed error message to be displayed when ExecuteNonQuery fails.

I'm using OleDb connection, so my guess is I have to use OleDbException or OleDbError to get a detailed error message.

Basically, the question is - how do I update this code to have a detailed error message IF ExecuteNonQuery fails?

string v1 = Request["v1"];                                    
string v2 = Request["v2"];
sql2 = "INSERT INTO table(one, two) VALUES('" + v1 + "', '" + v2  + "')";

System.Data.OleDb.OleDbConnection con2 = new System.Data.OleDb.OleDbConnection();
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data          Source=C:/Users/BB/Desktop/Database.mdb";
con2.Open();
OleDbCommand command = new OleDbCommand(sql2, con2);

try
{
    command.ExecuteNonQuery();
}
catch
{
    Response.Write("Error!");
    // detailed error message here?
}

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

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

发布评论

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

评论(2

z祗昰~ 2024-11-15 17:59:51

将您的 catch 更改为 catch (Exception ex) 然后您可以使用 Response.Write(ex.Message)

理想情况下,您会有不同的结果捕获不同的异常类型。这是捕获所有异常的桶方法。

Change your catch to be catch (Exception ex) and you can then use Response.Write(ex.Message)

Ideally, you would have different catches for different exception types. This is a bucket approach to catching all exceptions.

我还不会笑 2024-11-15 17:59:51

将 catch 更改为 catch (SqlException ex) 和 catch (Exception ex)

        catch (SqlException ex)
        {
            StringBuilder errorMessages = new StringBuilder();
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                errorMessages.Append("Index #......" + i.ToString() + Environment.NewLine +
                                     "Message:....." + ex.Errors[i].Message + Environment.NewLine +
                                     "LineNumber:.." + ex.Errors[i].LineNumber + Environment.NewLine);
            }
            WriteToLog(FileLog, "SQL Error message: " + errorMessages.ToString() + Environment.NewLine);
            WriteToLog(FileLog, "Process has been finished with errors." + Environment.NewLine);
        }
        catch (Exception ex)
        {
            WriteToLog(FileLog, "Error message: " + ex.Message + Environment.NewLine);
            WriteToLog(FileLog, "Error stack trace: " + ex.StackTrace + Environment.NewLine);
            WriteToLog(FileLog, "Process has been finished with errors." + Environment.NewLine);

Change your catch to catch (SqlException ex) and catch (Exception ex)

        catch (SqlException ex)
        {
            StringBuilder errorMessages = new StringBuilder();
            for (int i = 0; i < ex.Errors.Count; i++)
            {
                errorMessages.Append("Index #......" + i.ToString() + Environment.NewLine +
                                     "Message:....." + ex.Errors[i].Message + Environment.NewLine +
                                     "LineNumber:.." + ex.Errors[i].LineNumber + Environment.NewLine);
            }
            WriteToLog(FileLog, "SQL Error message: " + errorMessages.ToString() + Environment.NewLine);
            WriteToLog(FileLog, "Process has been finished with errors." + Environment.NewLine);
        }
        catch (Exception ex)
        {
            WriteToLog(FileLog, "Error message: " + ex.Message + Environment.NewLine);
            WriteToLog(FileLog, "Error stack trace: " + ex.StackTrace + Environment.NewLine);
            WriteToLog(FileLog, "Process has been finished with errors." + Environment.NewLine);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文