vb.net 未找到行时使用 CopyToDataTable() 避免异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18081797/
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
Avoid exception using CopyToDataTable() when no rows are found
提问by Harsh
Avoid exception using CopyToDataTable() when no rows are found I tried this code when no rows found then it gives me error The source contains no DataRows.
当没有找到行时使用 CopyToDataTable() 避免异常我在没有找到行时尝试了这个代码然后它给了我错误源不包含数据行。
ds.Tables.Add(dsDecEjID.Tables(0).Select(Cond).CopyToDataTable())
回答by Steven V
You didn't specify the error, but I'm guessing the select statement is returning null. Can't you use a simple null check?
您没有指定错误,但我猜 select 语句返回 null。你不能使用简单的空检查吗?
var table = dsDecEjID.Tables(0).Select(Cond);
if(table != null)
ds.Tables.Add(table.CopyToDataTable());
回答by Birkan AYDIN
table != nullnot working me, maybe someone like me, my solition is
table != null不工作我,也许像我这样的人,我的孤独是
var tableOb = tableMySqlSerialConn.Select(stringSelect);
if (tableOb.Count()>0)
{
tempTable =tableOb.CopyToDataTable();
}
回答by user2405987
If you are using C# 6.0, then you can use:
如果您使用的是 C# 6.0,则可以使用:
table?.CopyToDataTable()
回答by matth
Check if there are any rows in the table:
检查表中是否有任何行:
If dsDecEjID.Tables(0).Rows.Count > 0 Then
ds.Tables.Add(dsDecEjID.Tables(0).Select(Cond).CopyToDataTable())
End If

