Help with reading SqlDataReader

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I am new to vb.net using visual studio. I'm having a hard time trying to figure out the new syntax for vb.net

The following is code is what I'm trying to create in vb.net

Set cnGblBatch = New ADODB.Connectio
cnGblBatch.ConnectionString = "Provider=SQLOLEDB;Server=;Database=;UID=;PWD=;
cnGblBatch.Ope

Set rsBatch = cnGblBatch.Execute("select * from person"

Do While Not rsBatch.EO

sFirstName = rsBatch!FirstName
sMI = rsBatch!M
sLastName = rsBatch!LastNam
sAddr1 = rsBatch!Addres
sCity = rsBatch!Cit
sState = rsBatch!Stat
sZip = rsBatch!Zi

rsBatch.MoveNex

Loo

rsBatch.Clos

Any help would be appreciated
 
Dim cn as New SqlConnection("ConnectionStringHere")
Dim cmd as New SqlCommand("SQLStatement", cn)
Dim dr as SqlDataReader

dr = cmd.ExecuteReader

While dr.Read
sFirstName = dr.GetString(0) ' 0 is the index of the first columname in
your SQL Statement
sLastName = dr.GetString(1)
End While


beecherw said:
Hi,

I am new to vb.net using visual studio. I'm having a hard time trying to
figure out the new syntax for vb.net.
The following is code is what I'm trying to create in vb.net:


Set cnGblBatch = New ADODB.Connection
cnGblBatch.ConnectionString = "Provider=SQLOLEDB;Server=;Database=;UID=;PWD=;"
cnGblBatch.Open

Set rsBatch = cnGblBatch.Execute("select * from person")

Do While Not rsBatch.EOF

sFirstName = rsBatch!FirstName
sMI = rsBatch!MI
sLastName = rsBatch!LastName
sAddr1 = rsBatch!Address
sCity = rsBatch!City
sState = rsBatch!State
sZip = rsBatch!Zip

rsBatch.MoveNext

Loop

rsBatch.Close


Any help would be appreciated!

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
Hi Beecherew,

I give an alternative to Bills code because I saw you where using the
recordset method which looks more on the dataset method than the datareader.
The method from Bill works as well.

Dim cn as New SqlConnection("ConnectionStringHere")
Dim cmd as New SqlCommand("SQLStatement", cn)
Dim dr as SqlDataReader
dim ds as new dataset
dim da as new SQLDataAdapter(cmd)
da.fill(ds)
dim dr as datarow = ds.tables(0).rows(0)
sFirstName = dr("FirstName")
sMi = dr("MI")
etc.

I hope this helps?

Cor
 
Back
Top