【发布时间】:2013-06-10 07:51:03
【问题描述】:
所有,我正在尝试找到一种优雅的方式来从DataRow 中检索值。以下是我尝试过的方式。请审查它。我认为它还不够好。因为当我要检索的字段数量很大时,它会很冗长。有更好的主意吗?谢谢。
int result = -1;
if (row["intCol"] != null)
{
bool bresult = int.TryParse(row["intCol"].ToString(), out result);
}
更新
DB 中的intCol 数据类型是可为空的字符串。
更新
从Most efficient way to check for DBNull and then assign to a variable?找到它
干杯。
【问题讨论】:
-
如果
row["intCol"]总是一个整数,你可以这样做:int result = (int)row["intCol"]。否则,请先检查类型。 int->string->int 转换(我认为你在做什么)似乎是错误的 -
如果您知道应该存在什么类型 -
row.Field<int?>("intCol")(以避免DBNull失败) -
您好,很抱歉忘记指定
intCol的数据库类型。它是可为空的字符串。谢谢。