Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert smallint of t-sql to integer in c#?

Tags:

c#

sql

sql-server

I'm having problems converting smallint in t-sql to integer in C#.

Please can someone help me with a way round this?

Update #1

What i really trying to do is retrieve data from a column marked as smallint in sqlserver 2005 from a datareader into my application. Im sorry i'll was not really clear enough previously.

like image 455
Orson Avatar asked Dec 15 '10 11:12

Orson


2 Answers

Not quite sure what problems you are having as the range of numbers in smallint is a subset of the range of integer values.

The standard convert in c# should work:

int intFromSmallInt = Convert.ToInt16(smallint);

Is the error coming from an ORM?

like image 60
amelvin Avatar answered Sep 26 '22 13:09

amelvin


There is also the C# DataType "short", which is System.Int16 (a.k.a. "smallint" in SQL Server).

I prefer to use "short" just because I think it looks cooler and for no other reason.

Also, I'd use the following to pull your data (if it is nullable):

short? sVal = dr["ColName"] is System.DBNull ? null : (short?)(dr["ColName"]);
like image 23
MikeTeeVee Avatar answered Sep 26 '22 13:09

MikeTeeVee