我正在使用 SqlConnection
和 SqlCommand
。
如果出现任何 SqlException
,我必须捕获异常。
我使用了 using
语句,并在其中嵌套了一个 try/catch块
。以下是代码:
public static void LogError(string error, string message)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["connStringWeb"]))
using (SqlCommand cmd = new SqlCommand("INSERT INTO errorLogTable (errorTime, errorType, error) VALUES(@errorTime, @errorText, @errorMsg)"))
{
cmd.CommandTimeout = 300;
cmd.Connection = conn;
cmd.Prepare();
cmd.Parameters.AddWithValue("@errorTime", DateTime.Now);
cmd.Parameters.AddWithValue("@errorText", error);
cmd.Parameters.AddWithValue("@errorMsg", message);
try
{
conn.Open();
int i = cmd.ExecuteNonQuery();
}
catch { }
}
}
}
我的问题是,如果出现异常,我的
SqlConnection
和SqlCommand
会被处理掉吗?这种方法是否好处理呢?还是我应该使用传统的try/catch/finally
块来处理它?