1.Cannot exist more then an open reader for a given command, so you must close the reader after its usage.
2.You should use
DataReader
by using
try-finally
block like in the next example:
List<ErpCountry> resultList = new List<ErpCountry>();
DataReader reader = null;
try
{
IDbCommand dbCommand = new SqlCommand();
dbCommand.Connection = _session.DbConnection;
dbCommand.CommandText = string.Format("SELECT ID, Name from Countries");
if (_session.DbConnection.State == ConnectionState.Closed)
_session.DbConnection.Open();
reader = dbCommand.ExecuteReader();
while (reader.Read())
{
ErpCountry erpEntity = new ErpCountry();
erpEntity.ID = (int)reader["ID"];
erpEntity.Name = (string)reader["Name"];
resultList.Add(erpEntity);
}
}
finally
{
if (reader != null)
reader.Close();
_session.DbConnection.Close();
}