when to use IDataReader ? why not just read directly from datareader?

Last post 12-02-2007 6:24 PM by sahajMarg. 7 replies.

Sort Posts:

  • when to use IDataReader ? why not just read directly from datareader?

    12-01-2007, 10:05 AM
    • Loading...
    • sahajMarg
    • Joined on 05-03-2007, 2:59 PM
    • Posts 392

    I might be confused. I have never used IDataReader to read through the datareader.

    I normally do something like the following to read through a SqlDataReader.

    while (rdr.Read())
    {
    // get the results of each column
    string contact = (string)rdr["ContactName"];
    string company = (string)rdr["CompanyName"];
    	}
    My question is when do we do something like the following and what is the difference between the above and the below: Why to use IDataReader why not have ( SqlDataReader reader)?
    protected void ReadReader(IDataReader reader)
    {
    		string contact =   (string)rdr["ContactName"];
    string company = (string)rdr["CompanyName"];

     
     

     

  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-01-2007, 11:25 AM
    • Loading...
    • benrick
    • Joined on 07-27-2006, 3:12 PM
    • Kent, Ohio
    • Posts 340

    IDataReader is referring to an interface. Basically If your method accepts an IDataReader that means that it will accept anything that uses that interface. This means you can use any data reader you want basically. The method will accept your datareader because the datareader implements the IDataReader interface.

    The advantage of doing this is that the method is not specific to a specific type of data reader. You could also roll your own class that implements this interface.

    Basically an interface is just a set of stuff that needs to be implemented on anything using the interface. To learn more read on there are plenty of sites on the internet which can explain Interfaces in C#.

    http://msdn2.microsoft.com/en-us/library/aa664574(vs.71).aspx

    http://www.csharp-station.com/Tutorials/Lesson13.aspx

    I hope that helps,
    Brendan

    C. Brendan Enrick
    Brendan's ASP.NET Advice Blog

    Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
    Filed under: ,
  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-01-2007, 11:29 AM
    • Loading...
    • benrick
    • Joined on 07-27-2006, 3:12 PM
    • Kent, Ohio
    • Posts 340

    The SqlDataReader you are talking about is a class which implements IDataReader. This means that anywhere an IDataReader may be used you can use SqlDataReader.

    C. Brendan Enrick
    Brendan's ASP.NET Advice Blog

    Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-01-2007, 12:33 PM
    • Loading...
    • sahajMarg
    • Joined on 05-03-2007, 2:59 PM
    • Posts 392

    thanks a lot for your reply. making sense now. Is it possible for you to give me a quick example of a custom class implementing IDataReader. 

  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-01-2007, 2:13 PM
    • Loading...
    • benrick
    • Joined on 07-27-2006, 3:12 PM
    • Kent, Ohio
    • Posts 340

    This site has an interesting example of using an IDataReader interface for to look at machine processes.

    http://www.bearcanyon.com/dotnet/

    Here is a link to the sample download from that site http://www.bearcanyon.com/dotnet/ProcessListReader.zip

    Interfaces are very cool. For example SqlDataReader and XmlDataReader both implement the IDataReader interface. This means you might have a method called DisplayUserData and the method takes an IDataReader as a parameter. It will not matter whether the datareader is a SqlDataReader or an XmlDataReader it will still be able to display the data correctly.

    Have a great day!

    Brendan

    C. Brendan Enrick
    Brendan's ASP.NET Advice Blog

    Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-01-2007, 6:13 PM
    • Loading...
    • sahajMarg
    • Joined on 05-03-2007, 2:59 PM
    • Posts 392

     thanks, so that means that the following two blocs are exactly doing the same thing right?

     

    while (rdr.Read())
    {
    // get the results of each column
    string contact = (string)rdr["ContactName"];
    string company = (string)rdr["CompanyName"];
    	}
    Second Block
    protected void ReadReader(IDataReader reader)
    {
    		string contact =   (string)rdr["ContactName"];
    string company = (string)rdr["CompanyName"];

     
    } 

     

  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-02-2007, 6:00 PM
    Answer
    • Loading...
    • benrick
    • Joined on 07-27-2006, 3:12 PM
    • Kent, Ohio
    • Posts 340

    Well they are not exactly the same.The difference doesn't have to do with the reader you're using though. The difference between the two code blocks is that one is using a while loop and the other is a method. To make them the same they would look like these

    First Block

      

    protected void ReadReader(SqlDataReader rdr)
    {
    	while (rdr.Read())
    	{
    		// get the results of each column
    		string contact =   (string)rdr["ContactName"];
    		string company = (string)rdr["CompanyName"];
    	}
    }
     

    Second Block

      

    protected void ReadReader(IDataReader rdr)
    {
    	while (rdr.Read())
    	{
    		// get the results of each column
    		string contact =   (string)rdr["ContactName"];
    		string company = (string)rdr["CompanyName"];
    	}
    }
     

    I like the second block better simply because you call them both using the same code, but the second one is not forcing you to continue using the SqlDataReader. It will allow you to change later on. You call the functions using the following code.

    ReadReader(myReader);
    My reader could either be a SqlDataReader or an IDataReader of any kind for that matter. 

    I hope that makes sense,
    Brendan
     

    C. Brendan Enrick
    Brendan's ASP.NET Advice Blog

    Make sure you click "Mark as Answer" for any post which has helped you. This will give recognition to those helping others as well as earn you a point. It also helps people know which posts still need work.
  • Re: when to use IDataReader ? why not just read directly from datareader?

    12-02-2007, 6:24 PM
    • Loading...
    • sahajMarg
    • Joined on 05-03-2007, 2:59 PM
    • Posts 392

    benick thanks a lot for your answer...really. 

Page 1 of 1 (8 items)
Microsoft Communities
Page view counter