C# 检查数据集是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9172976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:14:35  来源:igfitidea点击:

Check dataset is empty or not

c#dataset

提问by JanOlMajti

This is working for me just fine. With if checks if dataset is empty or not. If so, return null value. But is the check of dataset right way or should i do some other way?

这对我来说很好。使用 if 检查数据集是否为空。如果是,则返回空值。但是检查数据集是正确的方式还是我应该做一些其他的事情?

 da2 = new SqlDataAdapter("SELECT project_id FROM project WHERE _small_project_id = '" + cb_small_project.SelectedValue + "' ORDER BY NEWID()", conn);
 ds2 = new DataSet();
 da2.Fill(ds2);
 DataRow[] rowProject = dt2.Select();

 if (ds2.Tables[0].Rows.Count == 0)
    cmd.Parameters["@_project_id"].Value = guidNull;
 else
    cmd.Parameters["@_project_id"].Value = rowProject[0]["project_id"];

采纳答案by Rafa? Warzycha

In my opinion the 'right' way is to check both:

在我看来,“正确”的方法是检查两者:

ds2.Tables.Count 

ds2.Tables[0].Rows.Count

回答by gimbar

I'd try check for:
ds2.HasChanges()
It should be true if any data has been added. For further information check here.

我会尝试检查:
ds2.HasChanges()
如果添加了任何数据,它应该是真的。欲了解更多信息,请点击此处

回答by joetheterm

You can use booland return true. For all tables in dataset

您可以使用bool并返回true. 对于所有表dataset

bool IsEmpty(DataSet dataSet)
{
    foreach(DataTable table in dataSet.Tables)
    if (table.Rows.Count != 0) return false;

    return true;
}

回答by manikandanlg

try this

尝试这个

 if (((System.Data.InternalDataCollectionBase)(ds.Tables)).Count != 0)
{
}
else
{
}

above code will work

上面的代码将工作

回答by Osama Rizwan

This Worked for me.... and won't give exception....

这对我有用......并且不会给出例外......

foreach (DataTable table in ds.Tables)
   if (table.Rows.Count != 0)
      table.Dispose();