这个遗留代码是从应用程序中的大量地方调用的:
public void DBCommand(string dynSQL, bool Silent)
{
checkConnection();
SqlCeCommand cmd = objCon.CreateCommand();
SqlCeTransaction trans = this.GetConnection().BeginTransaction();
cmd.Transaction = trans;
try
{
cmd.CommandText = dynSQL;
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
MessageBox.Show(
string.Format("DBCommand Except in DBConnection.DBCommand: {0}. InnerException: {1}", ex.Message, ex.InnerException));//TODO: Remove
try
{
trans.Rollback();
}
catch (SqlCeException)
{
// Handle possible exception here
}
// MessageBox.Show(
// string.Format("DBCommand Except in DBConnection.DBCommand: {0}. InnerException: {1}", ex.Message, ex.InnerException));//TODO: Remove
WriteDBCommandException(dynSQL, ex, Silent);
}
}
当加载特定文件并将其内容插入数据库时,我收到此消息五次。但异常的Message和InnerException总是空字符串。
我甚至(有点迷信或不合逻辑地)将MessageBox.Show()从一个位置移动到另一个位置以查看是否会产生任何差异(它没有)。
显示异常,但没有提供有关问题的任何有用信息;我已经看过(手动,或者更确切地说,我猜是光学地扫描)有问题的数据,这一切似乎没问题。
那么为什么这个异常信息在哭泣和哭泣,但没有交流问题是什么,就像一个绞痛而又无声的婴儿(张大嘴巴,眼泪流下来,但没有声音逃逸)?
好吧,我改编了ErikEJ的代码片段(不能使用HelpLink或StringIsNullOrEmpty,因为我在Alley Oop,Bertha Butt和Artie Shaw的时代使用了很久很久以前的工具):
// from method that throws the exception
catch (SqlCeException sqlfail)
{
MessageBox.Show(GetSQLCEErrorInfo(sqlfail));
}
catch (Exception ex)
. . .
public static string GetSQLCEErrorInfo(SqlCeException args)
{
SqlCeErrorCollection errorCollection = args.Errors;
StringBuilder bld = new StringBuilder();
Exception inner = args.InnerException;
if (null != inner)
{
bld.Append("\nInner Exception: " + inner.ToString());
}
// Enumerate the errors to a message box.
foreach (SqlCeError err in errorCollection)
{
bld.Append("\n Error Code: " + err.HResult.ToString("X",
System.Globalization.CultureInfo.InvariantCulture));
bld.Append("\n Message : " + err.Message);
bld.Append("\n Minor Err.: " + err.NativeError);
bld.Append("\n Source : " + err.Source);
// Enumerate each numeric parameter for the error.
foreach (int numPar in err.NumericErrorParameters)
{
if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
}
// Enumerate each string parameter for the error.
foreach (string errPar in err.ErrorParameters)
{
if ((null != errPar) && (errPar.Trim() != string.Empty)) //IsNullOrEmpty(errPar))
{
bld.Append("\n Err. Par. : " + errPar);
}
}
}
return bld.ToString();
}
......我看到以下内容:
Error Code: 80040E14
Message : There was an error parsing the query. [Token line number, Token line offset,, Token in error,,]
Minor Err.: 25501
Source : Microsoft SQL Server 2000 Windows CE Edition
Num. Par. : 1
Num. Par. : 47
Err. Par. " [
...然后:
Exception: SLQ Server CE does not support parallel transactions.
Location. DBConnection.GetInstance(siteNo)
(我两次都得到了两次);但为什么只有两次?它会插入几百条记录......人们会认为如果它会失败一次,它每次都会失败。
答案 0 :(得分:3)
你做错了,你需要在捕获一般异常之前捕获由ExecuteNonQuery引起的更专业的SqlCeException。对于SqlCeExcpetion,需要特殊处理,您可以使用类似的东西来获取所有相关信息:
public static string ShowErrors(System.Data.SqlServerCe.SqlCeException e)
{
System.Data.SqlServerCe.SqlCeErrorCollection errorCollection = e.Errors;
StringBuilder bld = new StringBuilder();
Exception inner = e.InnerException;
if (!string.IsNullOrEmpty(e.HelpLink))
{
bld.Append("\nCommand text: ");
bld.Append(e.HelpLink);
}
if (null != inner)
{
bld.Append("\nInner Exception: " + inner.ToString());
}
// Enumerate the errors to a message box.
foreach (System.Data.SqlServerCe.SqlCeError err in errorCollection)
{
bld.Append("\n Error Code: " + err.HResult.ToString("X", System.Globalization.CultureInfo.InvariantCulture));
bld.Append("\n Message : " + err.Message);
bld.Append("\n Minor Err.: " + err.NativeError);
bld.Append("\n Source : " + err.Source);
// Enumerate each numeric parameter for the error.
foreach (int numPar in err.NumericErrorParameters)
{
if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
}
// Enumerate each string parameter for the error.
foreach (string errPar in err.ErrorParameters)
{
if (!string.IsNullOrEmpty(errPar)) bld.Append("\n Err. Par. : " + errPar);
}
}
return bld.ToString();
}