datareader columns

B

brian shannon

Can you define a value from a datareader through column
idexes. For example:

dim myreader as sqldatareader
SQL = select fname, lname, address from personal.

//Datareader is initialized with the above SQL code

Instead of using the following to assign values from the
reader:

while myreader.read
txtTest1.text = myreader("fname")
txtTest2.text = myreader("lname")
txtTest3.text = myreader("address")
end while


txtTest1.text = myreader.index[0]
txtTest2.text = myreaderindex[1]
txtTest3.text = myreaderindex[2]

I am looking to programatically go through the reader and
programatically assign to text boxes.

Thanks
 
T

Teemu Keiski

Hi,

yes you can. You can also get column index with column name if you use
GetOrdinal method (e.g idea is to get once the indexes with column names &
GetOrdinal and then later just use indexes).
 
B

brian shannon

Thanks for the reply- if you have time could you show me an example of
the get ordinal method.
 
T

Teemu Keiski

Public Sub ReadMyData(myConnString As String)
Dim mySelectQuery As String = "SELECT OrderID, CustomerID FROM Orders"
Dim myConnection As New SqlConnection(myConnString)
Dim myCommand As New SqlCommand(mySelectQuery, myConnection)
myConnection.Open()
Dim myReader As SqlDataReader = myCommand.ExecuteReader()
Dim custIdCol As Integer = myReader.GetOrdinal("CustomerID")
Do While myReader.Read()
Console.WriteLine("CustomerID = {0}", myReader.GetString(custIdCol))
Loop
myReader.Close()
myConnection.Close()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top