I've been trying to understand why it's giving me this error because I'm not using SqlDataReader, I get that I'm using SQL Server and the Interface is returning the specific type but still I'm telling .NET to use IDataReader instead.
Here's the code:
public DataSet ExecuteSelectProcedure(string procedure, List<Parameter> parameters)
{
conexion.Open();
try
{
IDbCommand command = conexion.CreateCommand();
command.CommandType = CommandType.StoredProcedure;
command.Connection = conexion;
command.CommandText = procedure;
foreach (var parameter in parameters)
{
IDbDataParameter dataParameter = command.CreateParameter();
dataParameter.Direction = (System.Data.ParameterDirection) parameter.Direction;
dataParameter.Value = parameter.Value;
dataParameter.ParameterName = parameter.Name;
dataParameter.DbType = (DbType) parameter.Type;
command.Parameters.Add(dataParameter);
}
IDataReader result = command.ExecuteReader();
DataSet dataSet = new DataSet();
foreach (var table in (IEnumerable<IDataReader>) result)
{
DataTable dataTable = new DataTable();
dataTable.Load(table);
dataSet.Tables.Add(dataTable);
}
result.Close();
return dataSet;
}
finally
{
conexion.Close();
}
The error is when casting IEnumerable on the foreach loop. Thanks in advance…
Best Answer
It is using
SqlDataReader
, because that's the actual type thatcommand.ExecuteReader
is returning. You're then trying to cast that to anIEnumerable<IDataReader>
, but it's not clear why. Why did you expect that to work?You iterate over multiple tables (etc) in an
IDataReader
by callingNextResult()
. For example: