How to convert smallint of t-sql to integer in c#?
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 re开发者_Go百科ally clear enough previously.
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?
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"]);
int? value = (int?)(row["ColumnName"] as short?);
精彩评论