I am wondering what is the best practice and why. Can't find anything that clearly explains advantages and disadvantages.
FYI... I am using .Net
Which is better?
- Open a SQLConnection
- execute my SQL with a datareader
- loop through the results
- have my using statement close the reader
- have the using statement close the connection
OR
- Open a SQLConnection
- execute my SQL with a datareader
- have the using statement close the connection
- loop through the results
- close the reader
My thoughts on the second one is if you have a large result sets you aren't holding the connection open longer than you should. Is that correct and is that what I want to be doing.
edit
I have something similar to this.
using sq as new sqlconnection
>>connection and sproc stuff<<
using rd as new sqlreader
while rd.read
do some work
end while
end using
end using
doing this works too
dim rd as datareader
using sq as new sqlconnection
>>connection and sproc stuff<<
rd = sq.executereader
end using
while rd.read
do work
end while