I have a tinyint column in the database and I wish to convert it to Int32 for an SqlDataReader.
How do i go about it?
Edit #1
I recently had to do this.
int a = dataReader.GetByte(dr.GetOrdinal("ColumnName"));
#In Addition to Answer
SQL Server Data Type Mappings
bigint - GetInt64
binary - GetBytes
int - GetInt32
money - GetDecimal
rowversion - GetBytes
smallint - GetInt16
tinyint - GetByte
uniqueidentifier - GetGuid
...
For more info visit – SQL Server Data Type Mappings
What does it normally come back as – byte? If so, just do an unbox and then a convert:
or just let the conversion happen naturally:
or do the same with the strongly typed methods:
Adjust this to
sbyteorshortor whatever if I’m wrong about it mapping tobyte. You could do the conversion at the SQL Server side, but I’d personally do it at the client side instead, and keep the SQL simpler.