我編寫了一個支持 SQLite 的 CLI 應(yīng)用程序。為此,我使用SQLite 開發(fā)團(tuán)隊的 NuGet 包“ System.Data.SQLite ”。一切正常,但是如果出現(xiàn)異常,如何檢查“擴(kuò)展結(jié)果代碼(查看第 5 點) ”?我可以訪問 Enum SQLiteErrorCode 中的所有錯誤代碼,但在“ex.ResultCode”中始終是主要結(jié)果代碼(查看第 4 點)。例如try{ // DB actions ....}catch (SQLiteException ex){ // This is what i want because is clear and easy to read if (ex.ResultCode == SQLiteErrorCode.Constraint_Unique) { Debug.Write("SQLiteError: " + ex.Message); } // This works, but is not nice if (ex.ResultCde == SQLiteErrorCode.Constraint && ex.Message.Contains("UNIQUE")) { Debug.Write("SQLiteError: " + ex.Message); } throw ex;}有人可以幫我嗎?
1 回答

繁星coding
TA貢獻(xiàn)1797條經(jīng)驗 獲得超4個贊
該功能從 1.0.70.0 版本開始添加:
https://system.data.sqlite.org/index.html/doc/trunk/www/news.wiki
1.0.70.0 - 2011 年 4 月 22 日
通過 SetExtendedResultCodes()、ResultCode() 和 ExtendedResultCode() 添加了對 sqlite3_extended_result_codes()、sqlite3_errcode() 和 sqlite3_extended_errcode() 的支持。通過 SQLiteLogEventHandler() 添加了對 SQLITE_CONFIG_LOG 的支持。
要使用它:
conn.Open();
conn.SetExtendedResultCodes(true);
您可以捕獲異常:
if (ex.ResultCode == SQLiteErrorCode.Constraint_Unique)
{
Debug.Write("SQLiteError: " + ex.Message);
}
- 1 回答
- 0 關(guān)注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消