Solved

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

Posted on 2011-02-15
7
911 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now