CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

DataReader to DataTable Happy Ending

I'm doing some .Net and MySQL work and I've got a data access layer that does all the heavy SQL lifting.  One of the needs that came up was to convert a DataReader (IDataReader, really) to a DataTable.  In good XP style, I wrote “the quickest implementation that solves the business need” for creating a DataTable from an IDataReader.  This involved looping through the fields and was a bit cumbersome -- but it worked.  Again, in good XP style [Darrell Norton, correct me if I'm wrong], I went back and refactored the code after passing all my unit tests.  This time it included digging through documentation and weblogs on the DataReader-to-DataTable issue because I didn't like all the looping and verbosity of the code.  Rather quickly, this post by Roy Osherove turned up.

Roy was tackling the same issue and, in the comments to Roy's post, Josh (last name and additional info was not posted) pointed out the base DBDataAdapter class includes an overload of the Fill() method that includes a DataTable and a DataReader.  The OdbcDataAdapter (and all the provider specific adapters I checked out -- not even for SQL Server) don't include this overload -- but if you create your own class deriving from DBDataAdapter directly, you're all set!  No more loops and explicit DataColumns to create -- the .Net Framework has a solution baked in; the trick was finding the base DBDataAdapter to begin with.

My custom class required implementations to the 4 abstract methods in DBDataAdapter (CreateRowUpdatedEvent, CreateRowUpdatingEvent, OnRowUpdating,OnRowUpdated) -- but that only took a few minutes.  Add another minute or two to run the unit tests with happy green results, and I was all set.  I'd developed a much leaner solution.

Without the blog-o-sphere this would've been a tougher task; I would've been stuck weeding through the documentation to find a solution (or checking out books or online articles for the tidbit I needed).  Granted, maybe it would've occurred to more of you to check for base DataAdapter classes to see if they expose a richer set of Fill() method overloads, but that thought didn't occur to me.  To be certain, for future data access challenges, I'll be sure to keep the base data access classes in mind. 

Happy .Netting!



Comments

tomrowton said:

Maybe I am just too ignorant of the specifics of XP, but the way I work, it almost always makes more sense to a)think and b)look before slinging any code.

Given a similar situation, I probably would have done the search first and found the shortcut before doing it the "hard" way(looping).

Maybe it's my perl roots showing, but I'm damn lazy when it comes to solving problems that I think have a fairly high chance of having been solved by someone else before. Yes, there's a lot to be learned in doing it the hard way, and it never hurts you as a coder to do similar work twice - but as a contractor, I get paid in 15 minute increments; as a dad, I cherish every 15 minutes extra I can have at home - for those reasons, I lean towards getting it done well faster, and I have had good experience doing a (well written) google search before entering the part of the map that reads "Here be dragons."
# February 9, 2004 2:50 PM

grant said:

Excellent point Tom, but my style may be different than yours. I didn't perceive any "dragons" until after I reflected on the method and it had "settled" for a week or so.

The looping approach satisfied the immediate need (and the initial customer demo!), but in catching my breath and reviewing I decided to explore alternatives to my loop-centric approach.

I'm sure I'll review and improve on lots of code between now and product release; while I'd certainly like to, I don't expect to get the code perfect on the first try. #1 I just try to get it to work; #2 I try to make it lean and clean.

Maybe the customer doesn't even care whether the method was refactored or not . . . but I suppose I do.
# February 9, 2004 3:14 PM

Carlos said:

Could you share the code of your class. I'm new to .net in general but it seems that your code may be the most efficient.
# March 19, 2004 8:29 AM

Grant Killian said:

DataReader to DataTable sample code
# March 23, 2004 7:06 AM

Joe Sytniak said:

Be careful not to use this CustomDbAdapter approach with a datareader that contains multiple resultsets.
# April 15, 2005 9:18 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!