Read ExtendedResultCode from a OneLite DbException to identify the specific SQLite failure.
dotnet add package RecursiveAI.OneLite.DataRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using System.Data.Common; using RecursiveAI.OneLite.Data; using DbConnection db = new OneLiteConnection( "Data Source=:memory:"); db.Open(); using DbCommand cmd = db.CreateCommand(); cmd.CommandText = "CREATE TABLE users " + "(email TEXT UNIQUE)"; cmd.ExecuteNonQuery(); cmd.CommandText = "INSERT INTO users " + "VALUES ('a@example.com')"; cmd.ExecuteNonQuery(); try { cmd.ExecuteNonQuery(); } catch (DbException ex) when (ex is OneLiteDataException sqlite) { int primary = sqlite.ResultCode; int extended = sqlite.ExtendedResultCode; Console.WriteLine($"Primary: {primary}"); Console.WriteLine($"Extended: {extended}"); }
The database exists only in memory and the SQL input is in the program. The first INSERT succeeds. The second throws OneLiteDataException, which derives from DbException; the filter lets unrelated exceptions propagate.
Paste the code above into Program.cs of a new dotnet new console project and dotnet run, or unzip the example and dotnet run.
Inserting the same email twice prints:
Primary: 19 Extended: 2067
Variations
Catch the exact UNIQUE failure. Replace the example’s catch clause with:
catch (OneLiteDataException error) when (error.ExtendedResultCode == 2067) { Console.WriteLine("Email already exists."); }
Related recipes
The short answer
When a command uses the OneLite ADO.NET provider, match the caught DbException to OneLiteDataException. Read ExtendedResultCode for the specific failure and ResultCode for its family. SqliteExtendedErrorCode and SqliteErrorCode are aliases for the same values. Use the numeric properties to choose what to handle instead of matching the English error message.
What is the difference between a primary and an extended SQLite error code?
The SQLite result code list names 19 as SQLITE_CONSTRAINT and 2067 as SQLITE_CONSTRAINT_UNIQUE. A general constraint handler can use 19, but it should not assume that every failure in that family means a duplicate value. For example, a failed foreign-key constraint has a different extended code. Handle only the specific case your application knows how to recover from; leave other errors for the caller or show a separate diagnostic. If OneLite has no more specific code for the error, ExtendedResultCode equals ResultCode.
What does DbException.ErrorCode return for a SQLite error?
On OneLiteDataException, ErrorCode is an override that returns the primary result code, so in this example it reports 19, not 2067. The base DbException type gives you a common way to catch database errors, not a provider-independent SQLite code. If the connection can come from several providers, branch on its actual exception type before reading provider-specific codes. OneLiteDataException also exposes Detail for extra diagnostics; leave it as diagnostic text rather than using it to classify the failure.
If you catch SqliteException from Microsoft.Data.Sqlite rather than OneLiteDataException, see the Microsoft.Data.Sqlite and EF Core guide for the SQLitePCLRaw adapter.