0

我想编写一个例程,它应该能够在指定的时间内锁定表并在任何等待之前异步返回。

我写了以下代码

    public static void LockTable(string TableName, string connection )
        {
            var s = @"BEGIN TRAN  
                    SELECT 1 FROM " + TableName + @" WITH (TABLOCKX)
                    WAITFOR DELAY '00:00:55' 
                    ROLLBACK TRAN";

            SqlConnection myConnection = new SqlConnection(connection);

            try
            {
                myConnection.Open();
                SqlCommand myCommand = new SqlCommand(queryStr, myConnection);
                myCommand.CommandTimeout = 60;
                myCommand.ExecuteNonQueryAsync();
            }
            catch (SqlException e)
            {
                LogHelper.Error(queryStr);
                LogHelper.Error(e);
                throw e;
            }
            finally
            {
                myConnection.Close();
            }
}

但我没有得到想要的结果。myCommand.ExecuteNonQueryAsync();is not lock the table while myCommand.ExecuteNonQuery();locks the table but waiting for the specified time before return. 我希望它异步发生。任何帮助深表感谢。

4

0 回答 0