gpt4 book ai didi

c# - 从 DataRow 获取值(value)的最佳方式是什么?

转载 作者:行者123 更新时间:2023-11-30 20:57:12 25 4
gpt4 key购买 nike

总而言之,我正试图找到一种从 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? 找到它

干杯。

最佳答案

使用 Field , IsNull , SetField避免处理DBNull.Value的方法.

在您发布的示例中,您可能只需要 Field:

int? result = row.Field<int?>("intCol");
// if intCol is DBNull.Value, result will now be null.

关于c# - 从 DataRow 获取值(value)的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17019169/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com