当数据库托管在共享位置时,如何为sql依赖关系清理作业

时间:2015-04-27 14:46:51

标签: c# sql-server sqldependency

我们的数据库托管在ORCSWEB的共享位置。所以我没有权限执行以下脚本

KILL QUERY NOTIFICATION SUBSCRIPTION 26

ORCSWEB表示他们不会允许我们获得执行上述脚本的正确权限,因为他们认为这是他们数据库的安全问题。

我尝试运行以下脚本来清理我的c#程序中与sql依赖关系相关的转储数据,但不是100%成功。

以下这个正常运行以清除会话sys表

DECLARE @ConvHandle uniqueidentifier
DECLARE Conv CURSOR FOR
SELECT CEP.conversation_handle FROM sys.conversation_endpoints CEP
WHERE CEP.state = 'DI' or CEP.state = 'CD'
OPEN Conv;
FETCH NEXT FROM Conv INTO @ConvHandle;
WHILE (@@FETCH_STATUS = 0) BEGIN
    END CONVERSATION @ConvHandle WITH CLEANUP;
    FETCH NEXT FROM Conv INTO @ConvHandle;
END
CLOSE Conv;
DEALLOCATE Conv;

我的完整例行程序,使用并停留了许可。

private void ClearOldSubscriptions()
{
    using (var connection = new SqlConnection(connectionString))
    {
        using (var command = new SqlCommand())
        {
            string sql =
                ////@"DECLARE @UniqueTimeout AS int = 3586; " +
                @"DECLARE @SubscriptionId AS int; " +
                @"DECLARE @Sql AS varchar(max); " +
                @"DECLARE SubscriptionCursor CURSOR LOCAL FAST_FORWARD " +
                @"    FOR " +
                @"        SELECT id " +
                @"        FROM sys.dm_qn_subscriptions " +
                @"      WHERE database_id = DB_ID() " +
                @"            AND timeout = @UniqueTimeout " +
                @"OPEN SubscriptionCursor; " +
                @"FETCH NEXT FROM SubscriptionCursor INTO @SubscriptionId; " +
                @"WHILE @@FETCH_STATUS = 0 " +
                @"BEGIN " +
                @"    SET @Sql = 'KILL QUERY NOTIFICATION SUBSCRIPTION ' + CONVERT(varchar, @SubscriptionId); " +
                @"    EXEC(@Sql); " +
                @" " +
                @"    FETCH NEXT FROM SubscriptionCursor INTO @SubscriptionId; " +
                @"END";

            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = sql;
            command.Parameters.Add("@UniqueTimeout", SqlDbType.Int).Value = 432000;

            connection.Open();

            command.ExecuteNonQuery();
        }
    }
}

private void ClearNotificationQueue()
{
    using (var connection = new SqlConnection(connectionString))
    {
        using (var command = new SqlCommand())
        {
            string sql =
                @"DECLARE @Conversation AS uniqueidentifier; " +
                @"DECLARE ConversationCursor CURSOR LOCAL FAST_FORWARD  " +
                @"    FOR " +
                @"        SELECT CEP.conversation_handle FROM sys.conversation_endpoints CEP  " +
                @"        WHERE CEP.state = 'DI' or CEP.state = 'CD' " +
                @"     " +
                @"OPEN ConversationCursor; " +
                @"FETCH NEXT FROM ConversationCursor INTO @Conversation; " +
                @"WHILE @@FETCH_STATUS = 0  " +
                @"BEGIN " +
                @"    END CONVERSATION @Conversation WITH CLEANUP; " +
                @" " +
                @"    FETCH NEXT FROM ConversationCursor INTO @Conversation; " +
                @"END " +
                @"";

            command.Connection = connection;
            command.CommandType = CommandType.Text;
            command.CommandText = sql;

            connection.Open();

            command.ExecuteNonQuery();
        }
    }
}

0 个答案:

没有答案