我使用stored procedure call
创建了一个包含两个参数的IDataReader
。当我尝试读取值时,它会引发Invalid attempt to call FieldCount when reader is closed.
错误。我不是closing the connection
也不是using
。我不知道为什么我会收到这个错误。在immediate window
我没有看到reader.HasRows
属性,但是当我调试时,我可以在拨打电话时看到结果,但是当我想要阅读它时,它会抛出错误。
代码:
public bool UniqueColumnValuesCheckCall(ObservableCollection<String> selectedColumns, String selectedTableName)
{
bool uniqueValueExists = true;
String columns = String.Empty;
String result = String.Empty;
if (selectedColumns != null && selectedColumns.Count > 0)
columns = String.Join(", ", selectedColumns);
int commandTimeout = GetSQLCommandTimeOut();
IDataParameter[] parameters =
{
this.Helper.GetParameter("@columnName", columns),
this.Helper.GetParameter("@tableName", selectedTableName)
};
//I can see results while debugging.
IDataReader reader = this.Helper.WithCommandTimeout(commandTimeout).ExecuteReader(this.ConnectionString, "IX_FindUniqueColumnValues", parameters);
try
{
while (reader.Read()) // For some reason it throws error "Invalid attempt to call FieldCount when reader is closed." here
{
if (!reader.IsDBNull(0))
result = reader.GetString(0);
if (result.Equals("Selected columns have no unique values"))
uniqueValueExists = false;
else
uniqueValueExists = true;
}
}
catch (Exception)
{
throw;
}
return uniqueValueExists;
}