Results 1 to 13 of 13

Thread: DataReader

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    DataReader

    I have been studying about the DataReader in the msdn from a couple of hours and i habe seen that they are using this term:Read only Forward only a lot of times for the DataReader but i cant understand why?Why are DataReaders Read only Forward only?

    please explain with a simple example sir

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataReader

    A DataReader provides access to the result set of a query.

    You can only use the DataReader to read that data. It cannot be used to make any changes to the database. That's why they say that it is read-only.

    You cannot move randomly through the result set. Once you advance to a record you cannot then move back to the previous record. That's why they say that it is forward only.

    If you want your data to be read/write and/or you want random access to it, you should be using a DataAdapter and a DataTable rather than a DataReader.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataReader

    Also i saw in the msdn that by default,the datareader stores one row at a time in the memory.
    Quote Originally Posted by jmcilhinney View Post
    Once you advance to a record you cannot then move back to the previous record.
    as you said,advancing to a record you cannot then move back to the previous record;does it mean that advancing to a record,the previous record is deleted from the memory and cannot be retrieved again until the code for reading data source via the datareader is again executed?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataReader

    If you could retrieve the data from a previous record after advancing then that would be going backwards, wouldn't it? "Forward only" means exactly what it says.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataReader

    one more thing:
    the DataAdapter uses the fill method to add data to the DataSet from the data source and the fill method uses the DataReader object implicitly to do the needful.........can you please add something to this?

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataReader

    The statement pretty much speaks for itself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataReader

    but my confusion lies in the fact that when the fill method of the dataadapter is called to fill the dataset,then is it the selectcommand of the dataadapter that fills the dataset with the data which it returns or is it the datareader that returns the data to fill the dataset?

    Now i am getting confused by reading the msdn.........Please help

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataReader

    How do you use a DataReader? You create a Command, call ExecuteReader and then read the data, right?

    What is the SelectCommand of a DataAdapter? It's a Command. When you call Fill on a DataAdapter, it calls ExecuteReader on the SelectCommand, reads the data and puts it into a DataTable.

    If you wanted to you could bypass the Fill method and call ExecuteReader on the SelectCommand yourself, but why would you want to do that when the DataAdapter can do it for you?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataReader

    and what is the purpose of the dataadapter then?it just works as a bridge between the data source and the dataset and thats it?

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataReader

    one more thng:the datareader is responsible for returning the column names and the types that are used to create tables in the DataSet;how this is done?

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Dec 2009
    Posts
    904

    Re: DataReader

    Column types are created in the dataset as .NET Framework types.........what does it means?

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataReader

    Quote Originally Posted by HowTo View Post
    one more thng:the datareader is responsible for returning the column names and the types that are used to create tables in the DataSet;how this is done?
    If you want to know how to get schema information from a DataReader then check out the documentation for the the appropriate DataReader class. If you look at the members it exposes you should be able to tell fairly easily.

    If you're asking what the mechanism used under the hood is then that depends on the data source. That's a low level detail that you really don't need to care about as a .NET developer.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: DataReader

    Quote Originally Posted by HowTo View Post
    Column types are created in the dataset as .NET Framework types.........what does it means?
    In the database your columns might be data types like 'text' or 'varchar' or the like, depending on the column properties and the data source. When that data is retrieved into a .NET app though, all columns that contain text will be mapped to a DataColumn whose DataType is String. String is the .NET Framework type that stores text so all text columns map to that type. The same goes for other columns. It doesn't matter what their data source-specific data type is, but only what type of data they store, e.g. all columns containing dates map to DateTime, etc.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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