Results 1 to 10 of 10

Thread: Data Readers

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    8

    Data Readers

    Hello All!

    I'm a VB .NET programmer but I'm also doing some C# code as well. Could someone show me some code examples of how Data Readers are looped?

    Here's how I do this in VB .NET:

    con.Open 'Open connection

    dr = com.ExecuteReader 'Use Command to Execute Reader

    Do While dr.Read

    ComboBox.Items.Add(dr!CustomerID) 'Read field Customer ID Key

    End Loop
    con.Close
    con = Nothing 'Release object

    Here's some of the understanding of C# sharp so far:

    con.Open;

    dr = com.ExecuteReader;

    do while dr.Read

    // Method here?

    con.Close

  2. #2
    Frenzied Member vbNeo's Avatar
    Join Date
    May 2002
    Location
    Jutland, Denmark
    Posts
    1,994

    Re: Data Readers

    con.Open;

    dr = com.ExecuteReader();

    While(dr.Read()) {
    comboBox.Items.Add(dr!CustomerID);
    }

    con.Close();
    (not sure about this one though): con = null;
    "Lies, sanctions, and cruise missiles have never created a free and just society. Only everyday people can do that."
    - Zack de la Rocha


    Hear me roar.

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

    Re: Data Readers

    This:
    vb Code:
    1. con = Nothing 'Release object
    does not do what you think it does. You should be doing this:
    vb Code:
    1. con.Dispose()
    Also, I'm not 100% sure that C# supports this notation:
    vb Code:
    1. dr!CustomerID
    If it does then cool, but if it doesn't then use this:
    vb Code:
    1. dr("CustomerID")
    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

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Data Readers

    Shouldn't that be dr["CustomerID"]?

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

    Re: Data Readers

    Quote Originally Posted by penagate
    Shouldn't that be dr["CustomerID"]?
    Indeed.
    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

  6. #6
    New Member
    Join Date
    Apr 2007
    Posts
    4

    Re: Data Readers

    Thanks guys. I will give some of these a go.

    PS Never like database programming the best of times.

  7. #7
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Data Readers

    I used to hate database programming but really it's mostly because libraries insist on making it more complicated than necessary. .NET provides a large array of classes to ease data access but it can be hard to decide which ones you need (in other words, it almost provides too many). Once you get the hang of creating a basic data access layer you realise it's very simple and you can follow the same patterns in many of your applications.

  8. #8
    New Member
    Join Date
    Apr 2007
    Posts
    4

    Re: Data Readers

    Allot advice down under. Cheers!

    PS I'm from England by the way.

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

    Re: Data Readers

    Are you aware that you created and replied to this thread using two different user names?
    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

  10. #10

    Thread Starter
    New Member
    Join Date
    Apr 2007
    Posts
    8

    Re: Data Readers

    Indeed, had some internet problems along with email problems as well.

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