如何获取一个int值,而不是从一个TINYINT(1)列中的布尔? [英] How to retrieve an int value rather than a boolean from a TINYINT(1) column?
问题描述
我有一个MySQL列指定为:
I have a MySQL column specified as:
`type` TINYINT(1) NOT NULL DEFAULT '0'
列的目的是存储整数值不超过127,因为预计不会超过一个极少数对象类型。
The intent of the column is to store an integer value not to exceed 127 as there are not expected to be more than a very few object "types."
我一直保存2在现场为我行之一。
I've stored '2' in the field for one of my rows.
使用 SQLyog的,一个简单的 SELECT类型从表
产生正确的结果,2。
Using SqlYog, a simple SELECT type FROM table
yields the proper result, 2.
但是,使用连接器/网6.1.2(诚然这是一个有点过时了,因为目前的版本是6.5.4),会发生以下情况:
However, using the Connector/Net 6.1.2 (admittedly it's a bit out of date, as the current version is 6.5.4), the following happens:
var Temp = Reader["type"].GetType(); // equals "Boolean"
这类型的列的是的通常用于布尔值,但在这种情况下,我想要得到的整数值。下面未能产生预期的结果:
This type of column is usually used for Boolean values, but in this case I want to get the integer value. The following fails to yield the expected result:
int i = Reader.GetInt32("type"); // equals 1 (should be 2)
什么是正确的方式在.NET应用程序中使用连接器/网络获得的int值从TINYINT(1)列?
What's the proper way to get int values from a TINYINT(1) column using Connector/Net in a .NET app?
这是使用MySQL版本5.5.16 的
推荐答案
作为连接器的困惑,答案可能是这部分从的 MySQL的文档;数值类型的:
The answer for the Connector's confusion may be this part from MySQL docs; Numeric Types:
在MySQL 5.1中,一个 BIT
数据类型可用于存储位字段值。 (5.0.3之前, MySQL的内部$ P $点 BIT
为 TINYINT(1)
)
As of MySQL 5.0.3, a
BIT
data type is available for storing bit-field values. (Before 5.0.3, MySQL interpretsBIT
asTINYINT(1)
.) ...
或(更可能的),这部分从数字键入概述:
or (even more probable) this part from Numeric Type Overview:
BOOL,BOOLEAN
BOOL, BOOLEAN
这些类型同义词 TINYINT(1)
。零值被认为是假的。非零值被认为是真正的...
These types are synonyms for TINYINT(1)
. A value of zero is considered false. Nonzero values are considered true ...
这篇关于如何获取一个int值,而不是从一个TINYINT(1)列中的布尔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!