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 ro开发者_如何学Cw
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!
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.
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
精彩评论