Link to home
Create AccountLog in
Avatar of peter58
peter58

asked on

Database locking

Hi Guys

I have a big string which contains a whole lot of batch queries and I was wondering how to lock the database or even better specific tables then perform the batch queries and then unlock either the tables or the database. I am running on sql server 2006. Basic code is below not exact but just to show how I am currently running the queries.

Dim DatabaseConn As SqlConnection = New SqlConnection("Server=sdf;UID=sdg;PWD=dfb;DATABASE=dfb")
Dim SqlQuery As SqlCommand
DatabaseConn.Open()
SqlQuery = New SqlCommand(QueryString, DatabaseConn)
SqlQuery.ExecuteNonQuery()
DatabaseConn.Close()
Avatar of doobdave
doobdave

Hi,

I assume you meant SQL Server 2005, and not 2006!

Not too sure if there's any new features for this, but I believe what you're looking for is called a "locking hint" (refer to SQL Server Books Online for more information).

So your INSERT/UPDATE/DELTE, would look something like this:

INSERT INTO tblname WITH (TABLOCKX)
 (col1, col2, colN)
VALUES
 (val1, val2, valN)

Hope this helps.

Best Regards,

David
Avatar of peter58

ASKER

Is there no way to lock it before and after all the batch queries as I want to update or insert them all before anyone can access them.
ASKER CERTIFIED SOLUTION
Avatar of JRossi1
JRossi1

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Follow-up.  

I believe the default isolation level in a transaction is 'serializable' which means: "A range lock is placed on the DataSet, preventing other users from updating or inserting rows into the dataset until the transaction is complete."