简体   繁体   English

如何检查数据表是否包含anyrow

[英]How to check If datatable contains anyrow

I am trying to check IF datatable contains anyrow ,I am using below code to do this ,But now even if this GetReferralDrName() method does not return any value still control is going to foreach loop ,what is the right way to do this 我试图检查IF数据包是否包含anyrow,我使用下面的代码来执行此操作,但现在即使此GetReferralDrName()方法没有返回任何值仍然控制将转到foreach循环,这是正确的方法是什么

DataTable dt = DBHandling.GetReferralDrName();
            if (dt != null)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    cmbReferralDr.Items.Add(dr["LastName"].ToString() + " " + dr["FirstName"].ToString());
                } 
            }      

If there are no rows, there is no harm in getting to the foreach step. 如果没有行,那么进入foreach步骤是没有害处的。 As it will just jump over that when there are no records in the datatable. 因为当数据表中没有记录时它会跳过它。 But you could do this: 但你可以这样做:

DataTable dt = DBHandling.GetReferralDrName();
if (dt != null && dt.Rows.Count > 0)
{
    foreach (DataRow dr in dt.Rows)
    {
        cmbReferralDr.Items.Add(dr["LastName"].ToString() + " " + dr["FirstName"].ToString());
    } 
} 

I don't see any problem.So try to check dt.Rows.Count . 我没有看到任何问题。所以尝试检查dt.Rows.Count

if(dt!=null)
{
  if(dt.Rows.Count>0)
  {
    //Your Other Code
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM