System.DBNull问题 [英] System.DBNull problem

查看:92
本文介绍了System.DBNull问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

using (SqlDataReader rdr = cmdm.ExecuteReader(CommandBehavior.CloseConnection))
{
   if (rdr.HasRows == true)
   {
     foreach (System.Data.Common.DbDataRecord row in rdr)
     {
       if (row[7].GetType() != System.DBNull)
       {
          tBoxCheckingBal_pg5.Text = (string)row[7];    // <== runtime error    
       }
       if (row[8].GetType() != System.DBNull)
       {
          tBoxSavingsBal_pg5.Text = (string)row[8];      // runtime error       
       }

我收到编译时错误: DBNull是一种类型,在给定的上下文中无效 t。

I get a Compile time error: DBNull is a type and it is invalid in the given context.

如果我说 null 而不是DBNull,那么我会收到运行时错误,因为这些字段为空,它们是空的。

If I say null instead of DBNull, then I get a runtime error because those fields are empty, they are nulls.

我该怎么办?

推荐答案

你知道实际上有什么用吗?看:

You know what actually worked? Look:

if (row[7].GetType() == GetType())
{
   tBoxCheckingBal_pg5.Text = (string)row[7];      
}
if (row[8].GetType() == GetType())
{
   tBoxSavingsBal_pg5.Text = (string)row[8];            
}


很奇怪,但谁在乎呢?

Very bizarre but who cares?


这篇关于System.DBNull问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆