Simple DataReader to Business Object

  • Thread starter Thread starter J055
  • Start date Start date
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
 
Andrew,

You didn't show your RunProcedureReader code, but there are a couple of
command behaviors that you might want to check out.

For example, when you are retrieving a business object's properties using
its primary key, you MAY get a little better performance like this:

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection Or
CommandBehavior.SingleRow)

Kerry Moorman
 
Hi Andrew,

I agree with Kerry. But in my opinion, you can use either ways(DataTable or
DataReader). Because the Fill method which DataAdapter uses to fill the
DataTable is actually calling DataReader internally. There aren't much
performance differences here.

Kevin Yu
Microsoft Online Community Support

============================================================================
==========================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
============================================================================
==========================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Back
Top