with (nolock)

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is the 'nolock' a necessary precaution against locking when executing a
command?

cmd.CommandText = "SELECT szStoreName FROM Stores with (nolock) WHERE
lStoreNumber=5"
storename = cmd.ExecuteScalar
rd = cmd.ExecuteReader
 
The rules for NoLock apply the same here as elsewhere. However, are you
intentionally calling executescalar and then executereader?
 
W.G. Ryan
I don't understand your answer. Is Nolock necessary or unnecessary in this
case?
The code snippet was just there to elicit an answer regarding nolock. I
don't plan to executeScalar and ExecuteReader that way.
Arne.
 
Arne,

It is not necessary, though if you are trying to access a contentious
resource in the midst of another transaction, then you can use nolock to do
a dirty read. (ReadUncommitted). That is not an approach I personally
recommend. If you find yourself using NoLock to get around other
transactions, then the problem is the other transactions that take too long
to execute, not your nolock statement. In Sql2k5, you can use the snapshot
isolation level to get around even that problem in most circumstances.

The only good use I can think of nolock is admin level monitoring.
 
It's definitely not necessary. Sahil elaborated on it below and it's pretty
much what I was going to say (he just said it a little better).
 
Back
Top