Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

.net - How to retrieve an int value rather than a boolean from a TINYINT(1) column?

I have a MySQL column specified as:

`type` TINYINT(1) NOT NULL DEFAULT '0'

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."

I've stored '2' in the field for one of my rows.

Using SqlYog, a simple SELECT type FROM table yields the proper result, 2.

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)

What's the proper way to get int values from a TINYINT(1) column using Connector/Net in a .NET app?

This is using MySQL version 5.5.16

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Put that in your connection string to use tinyint as number

TreatTinyAsBoolean=false


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...