1.Your code is using to many
var
, and
using
statement.
2.After you execute the reader you have to use a
while
block to read all data from the reader for each row.
3.Better is to use try-catch-finally block like in the next example:
List<ErpPriceColumn > resultList = new List<ErpPriceColumn>();
IDataReader reader = null;
SqlConnection dbConnection = null;
try
{
dbConnection = new SqlConnection(connectionString);
IDbCommand dbCommand = new SqlCommand();
dbCommand.Connection = dbConnection;
dbCommand.CommandType = CommandType.StoredProcedure;
dbCommand.CommandText = "TimeSummary_ForInvoice";
dbConnection.Open();
reader = dbCommand.ExecuteReader();
while (reader.Read())
{
ErpPriceColumn erpEntity = new ErpPriceColumn();
erpEntity.ID = (int)reader["ID"];
erpEntity.Comment = (string)reader["Comment"];
erpEntity.BeforeTaxPrice = (0 == (byte)reader["BeforeTaxPrice"]);
erpEntity.UpdateDate = (DateTime)reader["UpdateDate"];
resultList.Add(erpEntity);
}
}
catch (SqlException exception)
{
throw new MyExceptionClass(exception.Message, exception);
}
finally
{
if (reader != null)
reader.Close();
dbConnection.Close();
}
return resultList.ToArray();