An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
bool in C# is a normal .NET System.Boolean. SQLite itself only has INTEGER/REAL/TEXT/BLOB, but the ADO.NET provider (Microsoft.Data.Sqlite or System.Data.SQLite) and Dapper sit on top and perform type mapping and conversions for you.
From the .NET side:
-
boolis a primitive .NET type whose default isfalseand which is commonly used as a flag and in conditionals likeif (testVar). - When working with databases that don’t have a native Boolean type, .NET data providers map between database primitives and .NET
booltransparently.
For SQLite specifically:
- SQLite only stores INTEGER, REAL, TEXT, and BLOB.
- The Microsoft SQLite provider maps a .NET
Booleanto SQLiteINTEGERwith values0or1. - Providers can also convert from TEXT values like
"true"/"false"toboolwhen reading, so the caller sees abooleven though the underlying storage is TEXT.
Dapper’s ExecuteScalar<T> relies on the underlying ADO.NET provider’s type conversion. When T is bool, it asks the provider to give a Boolean. The provider inspects the SQLite value (TEXT in this case) and converts it to false, so:
bool testVar = DBconnect.KVpair_Get_Value<bool>(queryKey);
works without needing bool.TryParse in user code.
This is expected behavior and is safe to rely on as long as:
- The column values are consistently valid Boolean representations for the provider (e.g.,
0/1,true/false, or whatever the provider supports). - The schema and data format are under control so unexpected strings don’t appear.
If there is a risk of arbitrary or malformed text in that column, reading as string and using bool.TryParse (or custom parsing) would give more explicit control and error handling. Otherwise, letting Dapper and the provider handle the conversion is standard and safe.
References: