This repository has been archived by the owner on Jul 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Type conversion
Bradley Grainger edited this page Feb 19, 2014
·
1 revision
The official System.Data.SQLite has some interesting type conversion rules when calling SQLiteDataReader.GetX
(for various values of X).
SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder { DataSource = @"C:\Temp\test.db" };
using (var connection = new SQLiteConnection(csb.ConnectionString))
{
connection.Open();
using (var cmd = new SQLiteCommand(@"create table Test(Value integer); insert into Test(Value) values
(32768),
(2147483647),
(9223372036854775807),
(-9223372036854775808);", connection))
{
cmd.ExecuteNonQuery();
}
using (var cmd = new SQLiteCommand(@"select Value from Test;", connection))
using (var reader = cmd.ExecuteReader())
{
reader.Read();
try { reader.GetInt16(0); } catch (OverflowException) { } // throws OverflowException
reader.Read();
try { reader.GetInt16(0); } catch (OverflowException) { } // throws OverflowException
reader.Read();
reader.GetInt16(0); // -1
reader.Read();
reader.GetInt16(0); // 0
}
}
SQLiteConnectionStringBuilder csb = new SQLiteConnectionStringBuilder { DataSource = @"C:\Temp\test.db" };
using (var connection = new SQLiteConnection(csb.ConnectionString))
{
connection.Open();
using (var cmd = new SQLiteCommand(@"create table Test(Value text); insert into Test(Value) values
('123'),
('65533'),
('65535'),
('65536');", connection))
{
cmd.ExecuteNonQuery();
}
using (var cmd = new SQLiteCommand(@"select Value from Test;", connection))
using (var reader = cmd.ExecuteReader())
{
reader.Read();
reader.GetChar(0); // '{' == (char) 123
reader.Read();
reader.GetChar(0); // '\uFFFD' = (char) 65533
reader.Read();
try { reader.GetChar(0); } catch (ArgumentException) { } // throws ArgumentException
reader.Read();
try { reader.GetChar(0); } catch (OverflowException) { } // throws OverflowException
}
}