[英]How to convert smallint of t-sql to integer in c#?
我在將T-sql中的smallint轉換為C#中的整數時遇到問題。
請有人幫助我解決這個問題嗎?
更新#1
我真正想要做的是從sqlserver 2005中標記為smallint的列中的數據從datareader檢索到我的應用程序。 對不起,我以前不太清楚。
不太確定您遇到了什么問題,因為smallint中的數字范圍是整數值范圍的子集。
c#中的標准轉換應該有效:
int intFromSmallInt = Convert.ToInt16(smallint);
錯誤來自ORM嗎?
還有C#DataType“ short ”,它是System.Int16(在SQL Server中又名“ smallint ”)。
我更喜歡使用“短”,因為我認為它看起來更酷,沒有其他原因。
此外,我將使用以下內容來提取您的數據(如果它可以為空):
short? sVal = dr["ColName"] is System.DBNull ? null : (short?)(dr["ColName"]);
int? value = (int?)(row["ColumnName"] as short?);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.