How to convert smallint from t-sql to integer in C #?

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

Please help me with this way?

Update # 1

I am really trying to extract data from a column marked as smallint in sqlserver 2005 from a datareader in my application. I wish I had been clear enough before.

+7
source share
3 answers

Not quite sure what problems you have, since the range of numbers in smallint is a subset of the range of integer values.

A standard converter in C # should work:

int intFromSmallInt = Convert.ToInt16(smallint); 

Is the error coming from ORM?

+9
source

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

I prefer to use β€œshort” just because, in my opinion, it looks cooler and not for any other reason.

Also, I would use the following to pull your data (if this value is null):

 short? sVal = dr["ColName"] is System.DBNull ? null : (short?)(dr["ColName"]); 
+8
source
 int? value = (int?)(row["ColumnName"] as short?); 
-4
source

All Articles