in

code for eternity !!!

community website for .net freaks ;-)

Technology

Cool Extension Methods for IDataReader Interface

I find it really annoying to use the GetOrdinal method of the DataReader class everytime I have to access data in a strongly typed manner using any of the Get[DataType] methods of the DataReader class. Have a look at below code:

    int employeeID = dr.GetInt32(dr.GetOrdinal("EmployeeID"));
    string firstName = dr.GetString(dr.GetOrdinal("FirstName"));
    bool enabled = dr.GetBoolean(dr.GetOrdinal("Enabled"));

It would be so cool if I could directly specify the table column names like below:

    int employeeID = dr.GetInt32("EmployeeID");
    string firstName = dr.GetString("FirstName");
    bool enabled = dr.GetBoolean("Enabled");

This would save me so much typing and make my code look so much cleaner. The power of Extension Methods make this possible :-) I created a new class named IDataReaderHelper which has 18 Extension Methods in all. You can simply pass the table column names instead of table column indexes to all these 18 Extension Methods, and these 18 Extension Methods would take care of the rest by calling the GetOrdinal method of the DataReader class internally :-). Have a look at the screen shot below:

Note: For readability purpose, above screen shot just highlights 3 of the 18 Extension Methods present in the IDataReaderHelper class. The 18 Extension Methods present in the IDataReaderHelper class are:

  • GetBoolean
  • GetByte
  • GetBytes
  • GetChar
  • GetChars
  • GetData
  • GetDataTypeName
  • GetDateTime
  • GetDecimal
  • GetDouble
  • GetFieldType
  • GetFloat
  • GetGuid
  • GetInt16
  • GetInt32
  • GetInt64
  • GetString
  • GetValue

You can find the entire source code for the IDataReaderHelper class as an attachment with this post available for download. To see all my posts on Extension Methods click here.

Update: Guys please dont forget to check out the Chris's comments (2nd from top) regarding performance issues when using the GetOrdinal method of the DataReader class inside a loop. Thanks for your excellent feedback Chris :-)

Update: Guys please dont forget to check out programatik's post here where he / she has compared performance (using GetOrdinal versus using int inside a loop). His / her post is in Portuguese so you can go to http://www.google.com/translate_t and translate programatik's post from Portuguese to English. It took me 30 minutes to figure out the language of this post ;-) Great work programatik ;-)

Cheers,
Raj

~~~ CODING FOR ETERNITY !!! ~~~


I would really appreciate votes / kicks for this blog post if you found it useful ;-)

  kick it on DotNetKicks.com     Receive Email Updates


Comments

 

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

April 15, 2008 3:55 PM
 

Chris said:

"Because ordinal-based lookups are more efficient than named lookups, it is inefficient to call GetOrdinal within a loop. Save time by calling GetOrdinal once and assigning the results to an integer variable for use within the loop." msdn.microsoft.com/.../system.data.sqlclient.sqldatareader.getordinal.aspx

May 1, 2008 7:32 AM
 

raj said:

Hi Chris,

Thanks for your excellent feedback. I have updated my post to point users to your comments. Appreciated :-)

Cheers,
Raj

~~~ CODING FOR ETERNITY !!! ~~~

May 2, 2008 7:02 AM
 

Benchmarking do dataReader.GetOrdinal() « Programatik - O local do estagi??rio de Inform??tica said:

Pingback from  Benchmarking do dataReader.GetOrdinal() « Programatik - O local do estagi??rio de Inform??tica

May 12, 2008 7:51 AM
 

code readers said:

Pingback from  code readers

May 20, 2008 11:16 AM
 

Damon Carr said:

The performance difference is irrelevant when compared to the total time. It is almost all out of process work.

Also, you will typically gain far more net net when you have code  that might be slower (but noticably bad is the point) yet more readable.maintainable.

Unless you are building real-time systems, server based multi-threaded transaction servers, etc. the entire 'general focus' on performance optimizations should not exist more times then not.

Force yourself to optimize your code at the end of your iterations and see the large savings in time, reduction in often unnecessary code and overall bandwidth improvement,

Kind Regards,

Damon Wilder Carr

http://damon.agilefactor.com

June 24, 2008 12:35 AM

Leave a Comment

(required)  
(optional)
(required)  
Add


StopGlobalWarming.org  
Powered by Community Server (Non-Commercial Edition), by Telligent Systems