2

我想知道通过 BLToolkit 从 MSSQL 服务器返回的 BIGINT 值中获取 long 的最佳方法,哪个会更好?

long.Parse(db.Parameter("@rowCount").Value.ToString());

或者

System.Convert.ToInt64(db.Parameter("@rowCount").Value);

?

4

6 回答 6

2

我会这样投:

long lngVal = System.Convert.ToLong(db.Parameter("@rowCount").Value);

在这种情况下,我之前不会将对象转换为 String,因为没有理由这样做。

于 2012-03-05T10:10:38.003 回答
1

我认为下面是根据需要检索数据的好方法:

long longValue = 0;
if(db.Parameter("@rowCount").Value!=null)
{
    long.TryParse(db.Parameter("@rowCount").Value.ToString(), out longValue);
    //longValue: contains the actual long data
}

由于异常非常昂贵,并且根据上面的代码处理异常。所以它将提供更好的方法。

谢谢

于 2012-03-05T10:53:43.953 回答
0
int nRowCnt = db.GetOrdinals("@rowCount");
db.GetInt64(nRowCnt);
于 2012-03-05T10:34:34.920 回答
0

第二种形式更好,因为它保持很长时间。由于二进制<->十进制转换,与字符串相互转换可能会丢失精度。

于 2012-03-05T10:08:36.037 回答
0

使用(long)db.Parameter("@rowCount").Value 或者(long?)db.Parameter("@rowCount").Value如果您的值可能为空。

于 2012-03-05T10:08:40.403 回答
0

一种方法是从字符串长类型转换

((long)(db.Parameter("@rowCount").Value.ToString()))
于 2012-03-05T10:09:47.950 回答