Solved

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

Posted on 2011-02-15
7
917 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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 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…
Suggested Courses
Course of the Month7 days, 9 hours left to enroll

632 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