Results 1 to 9 of 9

Thread: Returning datareader question

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    Returning datareader question

    Hi all,

    Had a datareader question.

    Code:
    Return myDataReader
    I have a function which is retuning a datareader to various sections of my website which is then being bound to dropdown lists, gridviews, etc. Normally the last line in a function when is the data reader and connection getting closed?

    Is it best practive to return a data reader or a dataset/datatable (or something even better than these two)?

    Thanks,

    Strick

  2. #2
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Returning datareader question

    Hey,

    If it were me, I would be returning a business object which represented the information that is in the reader. i.e. create a business object class with properties that represents all the information, and then loop through your data reader, creating an instance on this class. From there, you can then bind this list to the controls on your page. This means that you can leave the data reader where it belongs, in the DAL, rather than binding it directly to the UI.

    Gary

  3. #3
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Returning datareader question

    I agree with Gary but for your info you can close the reader & connection from the calling function like follows

    Code:
    function getReader() as sqldatareader
    'setup connection, command etc
    
    myReader = myCommand.executeReader(commandBehavour.closeConnection)
    return myReader
    end function
    Code:
    dim reader as sqldatareader = getReader()
    'use reader
    reader.close() 'reader & connection destroyed
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Returning datareader question

    Don't use Data Readers if you want to pass data around in your application. They follow a connected architecture. They keep the connection open for their lifetime and and will burden your application as well as your database server. Moreover if you are opening each reader in a new connection, you can imagine the hell yourself. You won't be able to keep more than a specific number of users connected to your application.

    Read data into a dataset using your Reader (or Data Adapter) and then pass that dataset around. Better way is still what gary told you already.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Returning datareader question

    I not sure I get your logic Pradeep,

    The web is disconnected so connections, data are built and destroyed each request unless you persist data in memory. Reader can be more efficent because they don't use memory where as datasets etc do.

    The only correlation is if your repetadely getting the same data in a reader in one request, it would be more efficient to use a in memory object in that case.
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Returning datareader question

    The tip would be more applicable to VB.NET applications rather than ASP.NET web applications. Nevertheless it should be equally useful for web applications too. Ideally you should be passing data around instead of Data Readers or connections.

    So how many requests will your application be able to handle simultaneously? Try a stress test both ways and see the result. I haven't tested this so can't comment much on this.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Returning datareader question

    Quote Originally Posted by Pradeep1210 View Post
    Ideally you should be passing data around instead of Data Readers or connections.
    I think this is the heart of the matter, and what everything boils down to. At the end of the day, DataReader's, Connection, Commands, DataAdapters, (in my opinion) should stay in the DAL where they belong.

    Gary

  8. #8
    Frenzied Member brin351's Avatar
    Join Date
    Mar 2007
    Location
    Land Down Under
    Posts
    1,293

    Re: Returning datareader question

    I love DALs until the customer starts changing the project requirements and I have to re-build it.

    So how many requests will your application be able to handle simultaneously? Try a stress test both ways and see the result
    A straight out reader will win out over datasets & DAL's with in memory objects in terms of resources used etc. There are lots of articles on this showing graphs and the like. That does not mean an adhoc approach to managing data is best, it's not it's terrable clutter of code on pages and hell to find runtime errors. Win server & IIS these days have heaps of resources and manage the app/connection pool very well so I agree with both of you - go the DAL. If a server didn't have enough resources to cope with my DAL I'd ask for 2 servers
    The problem with computers is their nature is pure logic. Just once I'd like my computer to do something deluded.

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Returning datareader question

    Quote Originally Posted by brin351 View Post
    If a server didn't have enough resources to cope with my DAL I'd ask for 2 servers
    That is always a good plan as well. Thinking about the architecture of your planned installation always helps when thinking about code segregation as well.

    Gary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width