Solved

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

Posted on 2011-02-15
7
912 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
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…

910 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

18 Experts available now in Live!

Get 1:1 Help Now