问题
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?
}
回答1:
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.
来源:https://stackoverflow.com/questions/6084760/how-to-get-detailed-executenonquery-error-message