如何获取详细的 ExecuteNonQuery 错误消息?
我正在处理我的第一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的
catch
更改为catch (Exception ex)
然后您可以使用Response.Write(ex.Message)
理想情况下,您会有不同的结果捕获不同的异常类型。这是捕获所有异常的桶方法。
Change your
catch
to becatch (Exception ex)
and you can then useResponse.Write(ex.Message)
Ideally, you would have different catches for different exception types. This is a bucket approach to catching all exceptions.
将 catch 更改为 catch (SqlException ex) 和 catch (Exception ex)
Change your catch to catch (SqlException ex) and catch (Exception ex)