Basically I need to check if a table is locked from my code.
I know if I run this directly on the DB;
SELECT * FROM 'tablename' SET LOCK_TIMEOUT 0
I can get an immediate error if a table is locked or none if it isn’t. However when I call this statement from my code it still seems to wait for a period of time for the lock to be released before returning. The code I’m using to call this query is;
SqlConnection conn = new SqlConnection(@"ConnectionString");
using(conn)
{
conn.Open();
var query = new SqlCommand("SELECT * FROM tablename SET LOCK_TIMEOUT 0", conn);
try
{
SqlDataReader reader = query.ExecuteReader();
// No lock
}catch (Exception)
{
// Deal with lock exception
}
}
I’d like to know straight away. Any suggestions? Or any better ways of doing this?
It’s not clear what goal you are trying to achieve, and for what purpose, but you are setting the lock timeout after the SELECT, whereas you need to set it before.