Solved

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

Posted on 2011-02-15
7
916 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

734 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