Reading all the Rows in a Column using DataReader

M

Mattyw

Hi

I have a sqlcommand that returns all the rows in a column and then pass
that to a datareader.

I am new to VS.Net and so far I can only return the first row in the
first column using

label1.text = reader(0).tostring

I know that this returns the column and that (1) would read the next
one...what I need to be ablet to do is read all the "rows" that are
returned in the query but to a string so I can use the results in
another query.

so far I have:

oraconnection.Open()
Dim reader2 As OracleClient.OracleDataReader
reader2 = oracommand.ExecuteReader()
reader2.Read()
Label1.Text = reader2(0).ToString
oraconnection.Close()

This returns the first row and sends it to the label1.text, how would I
get the next row?, I need to end up with a string with all the rows in
from that query....not just the first??

Thanks in Advance
Matt.
 
B

Brendan Reynolds

You'd need to append the result of subsequent reads to the string. Something
like this ...

Module Module1

Sub Main()

Dim connectionString As String
Dim connection As System.Data.OleDb.OleDbConnection
Dim strSQL As String
Dim command As System.Data.OleDb.OleDbCommand
Dim reader As System.Data.OleDb.OleDbDataReader
Dim result As String

Const delimiter As String = " "

connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=c:\dsdata\northwind.mdb;" & _
"Persist Security Info=False"
connection = New System.Data.OleDb.OleDbConnection(connectionString)
strSQL = "SELECT CategoryID FROM Categories"
command = New System.Data.OleDb.OleDbCommand(strSQL, connection)
connection.Open()
reader = command.ExecuteReader(CommandBehavior.CloseConnection)
If reader.HasRows Then
Do While reader.Read
If Not reader.IsDBNull(0) Then
result = result & " " & reader.GetInt32(0).ToString() &
delimiter
End If
Loop
result = result.Trim
Console.WriteLine(result)
Else
Console.WriteLine("no data")
End If
reader.Close()
Console.ReadLine()

End Sub

End Module
 

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

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top