How can you convert “tinyint” of t-sql to integer in c#?

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?

  • Warning: An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll.
  • How to use SQL variable to iterate XML nodes
  • Finding the Ultimate manager ID
  • select data up to a space?
  • Why date filed is not taking null but 1900-01-01?
  • @@SERVERNAME vs serverproperty('servername')
  • 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

  • SQL XML column - update child node value based on other nodes
  • Conditional sum using linq for Or condition
  • c# adapter.fill dataset not working with stored procedure even though I know the parameters are returning data?
  • Linq-to-sql failing on insert and update when this is a trigger attached
  • TSQL Calculate week number of the month
  • Print Prime Numbers with SQL query
  • 4 Solutions collect form web for “How can you convert “tinyint” of t-sql to integer in c#?”

    What does it normally come back as – byte? If so, just do an unbox and then a convert:

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

    or just let the conversion happen naturally:

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

    or do the same with the strongly typed methods:

    int x = reader.GetByte(column);
    

    Adjust this to sbyte or short or whatever if I’m wrong about it mapping to byte. 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.

    Use “SByte” works every-time For handling the Tiny Int problem

    int RetValue;
    RetValue = (int)(byte)dt.Rows[A][B]  // A = RowNo , B = 'tinyint' Column Name.
    

    To get a tinyint from a sql table i do the following:

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

    Where retorno is a int variable.

    I hope this help you.

    MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.