J
J055
Hello
I normally use DataTables to retrieve data but I thought I should probably
be using DataReaders to populate a Business Entity Object with a single
record at my DAL. I'm struggling to find some good examples on the subject.
Is there a better way of doing this? Is this faster then getting a
DataRow/DataTable? I plan to use this type of approach quite often in my
application so I thought I'd better ask if there's a more appropriate way of
doing it.
public ProductInformation Retrieve(int productID)
{
ProductInformation prod = new ProductInformation();
SqlParameter[] parameters = { new SqlParameter("@ProductID", SqlDbType.Int,
4) };
parameters[0].Value = productID;
SqlDataReader reader = RunProcedureReader("usp_Product_Select", parameters);
if (reader.HasRows)
{
prod.ProductID = (int)reader["ProductID"];
prod.ProductType = (string)reader["ProductType"];
prod.ProductName = (string)reader["ProductName"];
prod.ModifyDate = (DateTime)reader["ModifyDate"];
}
else
{
prod.ProductID = -1;
}
reader.Close();
return prod;
}
Many thanks
Andrew
I normally use DataTables to retrieve data but I thought I should probably
be using DataReaders to populate a Business Entity Object with a single
record at my DAL. I'm struggling to find some good examples on the subject.
Is there a better way of doing this? Is this faster then getting a
DataRow/DataTable? I plan to use this type of approach quite often in my
application so I thought I'd better ask if there's a more appropriate way of
doing it.
public ProductInformation Retrieve(int productID)
{
ProductInformation prod = new ProductInformation();
SqlParameter[] parameters = { new SqlParameter("@ProductID", SqlDbType.Int,
4) };
parameters[0].Value = productID;
SqlDataReader reader = RunProcedureReader("usp_Product_Select", parameters);
if (reader.HasRows)
{
prod.ProductID = (int)reader["ProductID"];
prod.ProductType = (string)reader["ProductType"];
prod.ProductName = (string)reader["ProductName"];
prod.ModifyDate = (DateTime)reader["ModifyDate"];
}
else
{
prod.ProductID = -1;
}
reader.Close();
return prod;
}
Many thanks
Andrew