views:

122

answers:

1

I have two applications, one updates a single table which has constant number of rows (128 rows) using SqlDataAdapter.Update method , and another application that select from this table periodically using SqlDataReader.

sometimes the DataReader returns only 127 rows not 128, and the update application does not remove or even insert any new rows, it just update.

I am asking what is the cause of this behaviour?

+1  A: 

Yep, datareader requires an open connection and doesn't pull all the rows at the time the query was opened so you are pulling data real time from the server. This is unlike the DataTable which queries and puts all the information in a cached object (disconnected recordset).

Kevin
So I can solve the problem by using SqlDataAdapter.Fill in the reading application instead of using SqlDataReader class??
Ahmed Said
Yes that should do it.
Kevin
Your other possibility is to put lock on the table, look through all the records with the datareader and put them into in memory objects and close connection when are done.
Kevin
How can I put a lock on all records using the SqlDataReader, Iam executing simple select sql statement select * from x
Ahmed Said
It's been a while since I have had to do this, but you can do it in sql code, SELECT * FROM Table WITH(ROWLOCK). You have to be careful though because this will stop connections from updating the data until the connection releases the lock. If you have multiple things connecting to the same table, they will probably have to wait while the reader finishes.
Kevin
" You have to be careful though because this will stop connections from updating the data until the connection releases the lock."so the connection releases the lock when reader closes??
Ahmed Said
When the reader closes, it will release the lock.
Kevin