SafeDataReader Overview

SafeDataReader Overview

Old forum URL: forums.lhotka.net/forums/t/2793.aspx


sct_nraab posted on Friday, April 27, 2007

This may be pretty elementary, but can someone give me an overview of the nuts and bolts of how the SafeDataReader works?

I don't understand how it can be used to populate several different objects at a time.

For instance, using the example from the book, where the SafeDataReader is populated by data retrieved from a stored procedure which executes two separate select statements, how does the business object differentiate between the records from the first statement and the second statement when instantiating?

ajj3085 replied on Friday, April 27, 2007

The safe data reader works in the same way as a standard datareader, except I think it knows about smartdates and will translate null values to 'safe' values (0 for ints, empty string for strings, etc).

So to get the second result set, you need to use the NextResult method on the underlying datareader.

RockfordLhotka replied on Friday, April 27, 2007

What I do in the book is standard DataReader behavior - it is not at all unique to SafeDataReader. If you look at how SafeDataReader is implemented in Chapter 5, you'll see that it is just a thin wrapper over any System.Data.IDataReader.

The NextResult() method on a datareader moves from the current result set to the next result set.

Skafa replied on Friday, April 27, 2007

To get multiple result sets, you execute multiple select statements in your procedure like this:

create procedure GetProjects
 @projectId int
as
begin
 select * from projects where projectid = @projectId
 select * from projectresources where projectid = @projectId
end

sct_nraab replied on Friday, April 27, 2007

Thanks to everyone.  Much clearer now.

david.wendelken replied on Friday, April 27, 2007

If you check out the CslaContrib project, you can find an extension of SafeDataReader (called SmartSafeDataReader) that handles additional classes (SmartInt16, SmartInt32, SmartInt64, SmartFloat, SmartBool, etc.).  Plus those extra classes.

 

 

Copyright (c) Marimer LLC