我想知道通过 BLToolkit 从 MSSQL 服务器返回的 BIGINT 值中获取 long 的最佳方法,哪个会更好?
long.Parse(db.Parameter("@rowCount").Value.ToString());
或者
System.Convert.ToInt64(db.Parameter("@rowCount").Value);
?
我想知道通过 BLToolkit 从 MSSQL 服务器返回的 BIGINT 值中获取 long 的最佳方法,哪个会更好?
long.Parse(db.Parameter("@rowCount").Value.ToString());
或者
System.Convert.ToInt64(db.Parameter("@rowCount").Value);
?
我会这样投:
long lngVal = System.Convert.ToLong(db.Parameter("@rowCount").Value);
在这种情况下,我之前不会将对象转换为 String,因为没有理由这样做。
我认为下面是根据需要检索数据的好方法:
long longValue = 0;
if(db.Parameter("@rowCount").Value!=null)
{
long.TryParse(db.Parameter("@rowCount").Value.ToString(), out longValue);
//longValue: contains the actual long data
}
由于异常非常昂贵,并且根据上面的代码处理异常。所以它将提供更好的方法。
谢谢
int nRowCnt = db.GetOrdinals("@rowCount");
db.GetInt64(nRowCnt);
第二种形式更好,因为它保持很长时间。由于二进制<->十进制转换,与字符串相互转换可能会丢失精度。
使用(long)db.Parameter("@rowCount").Value
或者(long?)db.Parameter("@rowCount").Value
如果您的值可能为空。
一种方法是从字符串长类型转换
((long)(db.Parameter("@rowCount").Value.ToString()))