如果单元格可能为空,从数据行读取的正确方法是什么

发布于 2024-08-12 00:29:18 字数 261 浏览 5 评论 0原文

我有以下代码,如果数据行(dr)中的列为空,它似乎会崩溃。从数据行中解析出值并处理空检查的正确方法是什么?

Person person = new Person()
{
    FirstName = dr["FirstName"].ToString(),
    LastName = dr["LastName"].ToString(),
    BusinessPhoneNumber = dr["BusinessPhone"].ToString(),

I have the following code which seems to blow up if the column in the datarow (dr) is null. what is the correct way to parse out the value from the data row and handle null checks?

Person person = new Person()
{
    FirstName = dr["FirstName"].ToString(),
    LastName = dr["LastName"].ToString(),
    BusinessPhoneNumber = dr["BusinessPhone"].ToString(),

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

鸩远一方 2024-08-19 00:29:18

如果该列的类型为字符串,但可以为空,那么尝试:

// FirstName must allow null
FirstName = dr["FirstName"] as string

// FirstName would be empty for a NULL in the database
FirstName = (dr["FirstName"] as string) ?? string.Empty 

If the column is of type string, but is nullable, what about trying:

// FirstName must allow null
FirstName = dr["FirstName"] as string

or

// FirstName would be empty for a NULL in the database
FirstName = (dr["FirstName"] as string) ?? string.Empty 
醉生梦死 2024-08-19 00:29:18

我相信,数据单元格为空不能成为您问题的原因。也许该列不存在,或者发生了任何其他错误,或者 DataRow 本身为空,您应该处理该问题。查看异常 - “似乎爆炸”并不是对您的问题的有效描述。

下面解释一下,同时也为大家解答一下标题中的问题。

如果列值为 null,则返回对象 System.DBNull.ToString() 返回空字符串,而 (string)作为字符串返回null

因此,检查返回的项目是否为 ==null 是没有意义的,因为它永远不会计算为 true

  • 如果您接受空字符串作为结果,则您的代码已经是最佳的,无需处理 DBNull 情况。
  • 如果您想获取 null,请将 .ToString() 更改为 as string
  • 如果您想以任何其他方式处理它,请在 之后对目标变量使用 if (dr.IsNull("FirstName")) 或执行 ==null >作为字符串

I believe, the data cell beeing null cannot be the reason for your problem. Maybe the column doesn't exist, or any other error happened, or the DataRow itself is null and you should handle that. Look at the exception - "seems to blow up" is not a valid description of your problem.

The following explains that, but will also answer the question from the title for everyone else.

If the column value is null, an object System.DBNull is returned, and .ToString() returns an empty string, while (string) or as string return null.

So it makes no sense to check the returned item for ==null because that won't ever evaluate to true.

  • If you accept empty strings as result, your code is already optimal, no need to handle the DBNull case.
  • If you want to get null, change .ToString() to as string.
  • If you want to handle it in any other way, use if (dr.IsNull("FirstName")) or do ==null on the target variable after the as string.
舞袖。长 2024-08-19 00:29:18

dr["FirstName"].ToString() 有时会抛出空指针异常,因为您尝试访问空对象上的 ToString。除此之外,字符串可以为空,所以实际上,如果您知道它是一个字符串,并且不介意它为空,那么这样做是安全的

FirstName = (String) dr["FirstName"]

dr["FirstName"].ToString() will occasionally throw null pointer exceptions because you're trying to access ToString on a null object. Other than that, strings can be null, so really, if you know it's a string, and you don't mind that it's null, it'd be safe to do

FirstName = (String) dr["FirstName"]
简单爱 2024-08-19 00:29:18

将列和默认值传递给辅助函数,该函数检查是否为 null,如果为 null,则返回默认值;如果不为 null,则返回列值。

Pass the column, and a default value to a helper function that checks for null and returns the default if null, or the column value if not null.

走走停停 2024-08-19 00:29:18

如果列为空,您可以使用以下命令返回空字符串。

FirstName = string.format("{0}", dr["FirstName"])

You can use the following to return a blank string if the column is null.

FirstName = string.format("{0}", dr["FirstName"])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文