Hi fellow experts,
I assume that this is a standard problem, but I haven't found an ideal answer to it.
Simplified, I have a table Persons with columns PersonID and Field1. Right now this table (in real life) contains 50.000 records and is about 200 MB large (with more fields, of course). in VB.net I create two connections, do a SELECT * FROM MyTable WHERE Field1='A' and loop through the connected datareader. In the loop, I do some computations and execute a "UPDATE MyTable SET Field1='B' WHERE PersonID=@PersonID" on the second connection, where @PersonID is fetched from the datareader.
In code, this looks like that:
cmd1.Connection = conn1
cmd2.Connection = conn2
cmd1.CommandText = "SELECT PersonID, Staat, ...somefields... FROM Personen where Staat IS NULL"
dr = cmd1.ExecuteReader()
Do While dr.Read
[.. do some calculations ..]
cmd2.CommandText = "UPDATE Personen SET ...somefields..., Staat=@Staat WHERE PersonID=@PersonID"
cmd2.Parameters.Add("@PersonID", Data.SqlDbType.Int).Value = dr("PersonID")
cmd2.Parameters.Add("@Staat", Data.SqlDbType.Int).Value = 1
The problem is, that the UPDATE command times out and never executs properly. I'm not surprised, because I'm modifying the data while it's being read by the datareader. Surely some kind of locking taking place here.
What would be the best way to loop through data, work on it, and write the modified records back?
Thank you for your support,