Solved

How to Use a DataReader to Read in Data From a SQL database using VB.NET 2010

Posted on 2011-02-15
7
915 Views
Last Modified: 2012-05-11
I was wondering if someone could explain to me how to use a datareader in VB.NET to read in data from a SQL database to the VB.NET code that is written. (Please use an example in the explanation so that I could see how the DataReader is being used.)
0
Comment
Question by:thenthorn1010
  • 4
  • 2
7 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 34899425
Are you trying to do something specific, or do you just need a basic example?

This article provides a sample of its usage if that's what you're after:

    http://www.developerfusion.com/article/4278/using-adonet-with-sql-server/2/
0
 
LVL 21

Expert Comment

by:silemone
ID: 34899452
You will use a SQL Command and then you will executeDateReader.  Then the contents of what's returned will be stored in the dataReader.

The data reader is  a one-way, forward-only method of reading data.  That means if you have a control that takes item[1] and another control that takes item[3],  you have to make sure the control that takes item[2], even if its the last control on the page is loaded because once item[2] is passed by, you can't go back to it...unless you reiterate through the whole DataReader again.
0
 

Author Comment

by:thenthorn1010
ID: 34899479
I am attempting to write out data that I have in a class by first populating the data using the DataReader to pass the values to values in a class. (Please see the attached classes that I have provided to see what data I am writing out.)

I am a bit new to the DataReader, as I am used to binding fields to the database and VB.NET as opposed to doing it with the DataReader. Let me know if you have any questions on the code. (The EDIAckHeader is the parent class to the EDIAckAddress class.)
EDIAckAddress.vb
EDIAckHeader.vb
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:silemone
ID: 34899489
Here's a basic example:

Dim conn As New SqlConnection(connectionString)
Dim comm As New SqlCommand("select * from mytable", conn)
comm.Connection.Open()
Dim r As SqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
While r.Read()
      Console.WriteLine(r.GetString(0))
End While
r.Close()
conn.Close()
0
 
LVL 21

Accepted Solution

by:
silemone earned 250 total points
ID: 34899525

Dim conn As New SqlConnection(connectionString)
Dim comm As New SqlCommand("select * from mytable", conn)
comm.Connection.Open()
Dim r As SqlDataReader = comm.ExecuteReader(CommandBehavior.CloseConnection)
While r.Read()
            strRecordType = r["fieldname"]
            strCustomerNumber = r["fieldname"]
            strChangeFlag = r["fieldname"]
            strOrderNumber = r["fieldname"]
           strOrderDate = r["fieldname"]
            strShipVia = r["fieldname"]
            strCustomerPONumber = r["fieldname"]
            strCustomerPOReleaseNumber = r["fieldname"]
            strPODate = r["fieldname"]
            strSystemDate = r["fieldname"]
            strSystemTime = r["fieldname"]
            strGSSenderID = r["fieldname"]
            strTD5DNSNumber = r["fieldname"]
            strTradingPartnerName = r["fieldname"]
            strDocument_TransactionID = r["fieldname"]
            strReceiverID = r["fieldname"]
            strSenderID = r["fieldname"]

           ''Where fieldname is the columname from db

End While
r.Close()
conn.Close()

Open in new window

0
 

Author Closing Comment

by:thenthorn1010
ID: 34899556
Thanks for the help. This really showed me how the data would be written and will help me output the data effectively to the correct code to utilize these classes.
0
 
LVL 21

Expert Comment

by:silemone
ID: 34900025
Happy to share

Cheers
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
System.net.Sockets Error 5 38
transition to visual .net from vb6 5 57
Crystal reports login after closing form 3 34
VB.net Open video relating to control 2 29
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question