user185998
user185998

Reputation:

Need recordset behavior in VB.NET w/ADO help

VB.NET newbie here.

I've learned (through this site) how to negotiate a dataset via:

For Each dRow In quanDS.Tables(0).Rows
  'do something to each row
Next

I now need to figure out now to loop through a subset of the records returned in the dataset - here's a VB6 example I need to convert:

strSQL = "select * from tblDQ order by xid, xcode, xDOS"
rsMaster.Open strSQL, conDB, adOpenDynamic, adLockOptimistic
rsMaster.MoveFirst

Do While Not rsMaster.EOF
    strxID = Trim(rsMaster!xID)
    strxCode = Trim(rsMaster!xcode)
    dblQuan = rsMaster!units
    Do While Trim(rsMaster!xID) = strxID And Trim(rsMaster!xcode) = strxCode
        rsMaster!unitdif = rsMaster!units - dblQuan
        rsMaster.Update
        dblQuan = rsMaster!units
        rsMaster.MoveNext
        If rsMaster.EOF Then
            Exit Do
        End If
    Loop
Loop

rsMaster.Close

Any help at all would be appreciated!

Upvotes: 1

Views: 1783

Answers (2)

rjrapson
rjrapson

Reputation: 1997

It would be pretty straight forward to convert that to a SQLDataReader to replace the recordset. Basically, the syntax is

    using conn as new sqlconnection({connection string})
   using cmd as new sqlcommand("select * from tblDQ order by xid, xcode, xDOS", conn)
      cmd.connection.open()
      using reader as SQLDataReader = cmd.ExecuteDataReader()
          while reader.read
                  do your thing here, referencing reader("field")
          end while
      end using  'dispose of the reader
    end using  'dispose of teh command
end using  'close and dispose of the connection

Upvotes: 1

John Saunders
John Saunders

Reputation: 161773

You could translate that directly. The rs!fieldName syntax translates to row("fieldName") in VB.NET.

It would be easier if you were using LINQ, though.

Upvotes: 1

Related Questions