How can you convert "tinyint" t-sql to integer in C #?

I have a tinyint column in the database and I want to convert it to Int32 for SqlDataReader .

How should I do it?

Edit # 1

I had to do this recently.

 int a = dataReader.GetByte(dr.GetOrdinal("ColumnName")); 

# In addition to the answer

SQL Server data type mapping

 bigint - GetInt64 binary - GetBytes int - GetInt32 money - GetDecimal rowversion - GetBytes smallint - GetInt16 tinyint - GetByte uniqueidentifier - GetGuid ... 

For more information, visit SQL Server Data Type Mapping.

+54
c # sql-server
Dec 17 '09 at 14:19
source share
4 answers

How is it usually returned as a byte? If so, just do unbox and then convert:

 (int)(byte) reader["column"]; 

or just enable the conversion in a natural way:

 int x = (byte) reader["column"]; 

or do the same with strongly typed methods:

 int x = reader.GetByte(column); 

Adjust this value to sbyte or short or something else if I am mistaken regarding its comparison with byte . You could do the conversion on the SQL Server side, but I would personally do it on the client side instead and simplify the SQL.

+79
Dec 17 '09 at 14:24
source share

Using "SByte" works every time. To solve the problem of Tiny Int.

+3
Dec 12 '14 at 16:53
source share
 int RetValue; RetValue = (int)(byte)dt.Rows[A][B] // A = RowNo , B = 'tinyint' Column Name. 
0
Nov 21 '11 at 13:57
source share

To get tinyint from sql table, I do the following:

 retorno = Convert.ToInt32(dr.GetByte(dr.GetOrdinal("StepStatus"))); 

If retorno is an int variable.

Hope this helps you.

0
Oct 22 '14 at 20:17
source share



All Articles