如何获取详细的ExecuteNonQuery错误消息? [英] How to get detailed ExecuteNonQuery error message?

查看:257
本文介绍了如何获取详细的ExecuteNonQuery错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我不知道如何在ExecuteNonQuery失败时如何显示详细的错误消息.

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

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

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

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

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?
}

推荐答案

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.

这篇关于如何获取详细的ExecuteNonQuery错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆