这一行:
searchParamsTable.Select(string.Format("TABLE = {0} AND FIELD = '{1}'", tableNumbers[i], fieldName[i]))[0]["VALUE"] = wildcardedSearchString;
在 C# 中返回数组索引越界异常。我怎样才能写这个不抛出错误?或者,如果没有返回记录以继续执行代码,是否有其他方法可以编写此代码?我在三个不同的表中搜索结果,使用带有“i”的行作为表和字段名称的增量。
最佳答案
这不完整,我们怎么知道tableNumbers
、fieldName
和wildcardedSearchString
是什么?可能会在 tableNumbers[i]、fieldName[i] 或选择的 [0]
索引器处抛出异常。
我假设选择不返回行,所以 [0]
将抛出 IndexOutOfBounds
异常。
您可以使用 Steves 方法或使用更具可读性和功能的 Linq-To-Dataset:
IEnumerable<DataRow> rows = searchParamsTable.AsEnumerable()
.Where(r => r.Field<String>("TABLE") == tableNumbers[i]
&& r.Field<String>("FIELD ") == fieldName[i]);
if(rows.Any())
{
// do something with the rows, for example create a new DataTable from the result:
DataTable tblSearch = rows.CopyToDataTable();
}
关于c# - 数据表选择返回数组索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620012/