当我从类型'诠释'的SQLite的列中选择,我可以转换为.NET INT但是当我从“整数”列中选择我不能 [英] When i select from sqlite column of type 'int' I can cast to .net int but when I select from 'integer' column I cannot

查看:212
本文介绍了当我从类型'诠释'的SQLite的列中选择,我可以转换为.NET INT但是当我从“整数”列中选择我不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是System.Data.SQLite,从这里一列的类型为'整'SQLite数据库表中选择,当我做这样的事情:

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)reader["myColumn"];



失败。问题不在于该值为null;该列不能为空。如果我更改列的数据类型为INT,那么它工作正常。在列中的值是'2','3','4',等;没有什么非常大的。

it fails. The problem is not that the value is null; the column is not nullable. If I change the data type of the column to 'int' then it works fine. The values in the column are '2', '3', '4', etc.; nothing very big.

任何人都知道,如果这是预期的行为?

Anyone know if this is expected behaviour?

推荐答案

如所提到的其他回答者,SQLite的整数被存储在1,2,3,4,6,或8个字节。但是,你不会得到溢出或超出范围的异常。

As the other answerer mentioned, SQLite integer is stored in 1, 2, 3, 4, 6, or 8 bytes. However, you won't get overflow or out of range exceptions.

在这种情况下,(INT)是一个演员,而不是转换。如果阅读器[] 没有返回整数类型的对象,如果它的返回数值类型,你会得到一个转换异常,不管它包含的价值。

In that context, (int) is a cast, not a conversion. If reader[] didn't return an object of type integer, if it's returning a different numeric type, you will get a cast exception, regardless of the value it contains.

根据SQLite的整数有效值的范围,我猜想,它的返回值作为64位整数,。为了验证,试试这个:

Based on the range of valid values for SQLite integer, I'd guess that it's returning the value as a 64-bit integer, long. To verify, try this:

object x = reader["myColumn"];
Debug.WriteLine(x.GetType().Name);

这篇关于当我从类型'诠释'的SQLite的列中选择,我可以转换为.NET INT但是当我从“整数”列中选择我不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆