We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Database locking

peter58
peter58 asked
on
Medium Priority
220 Views
Last Modified: 2010-04-23
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()
Comment
Watch Question

Commented:
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

Author

Commented:
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.
Commented:
I think utilizing a transaction would give you what you are looking for.  

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnadonet/html/transact.asp 

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
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."  

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.