When i select from sqlite column of type 'int' I can cast to .net int but when I select from 'integer' column I cannot

后端 未结 3 444
独厮守ぢ
独厮守ぢ 2021-02-09 00:22

I\'m using System.Data.SQLite, selecting from a sqlite database table where a column has type \'integer\', and when I do something like this:

int x = (int)reade         


        
3条回答
  •  情话喂你
    2021-02-09 00:57

    The exception occurs because datareader.Item[] returns object.

    The problem is due to boxing/unboxing. It's nothing to do with the database itself...

    long a = 1; 
    int b = 2; 
    object objectA = a; 
    object objectB = b; 
    Console.WriteLine((int)a); 
    Console.WriteLine((long)b); 
    Console.WriteLine((int)objectA); 
    Console.WriteLine((long)objectB); 
    

    Console outputs 2 then 1, but will throw an exception on the cast of objectA, objectB. This is a characteristic of .NET nothing to do with ODBC drivers.

提交回复
热议问题