从CLR存储过程接收Service Broker

时间:2010-10-18 19:37:27

标签: .net sql-server service-broker

我正在尝试将此T-SQL存储过程移至CLR过程,但是有一个Service Broker特定命令,我不知道如何实现:

DECLARE @msgBody XML    
DECLARE @dlgId uniqueidentifier

;RECEIVE top(1) 
        @msgBody    = message_body,      
        @dlgId      = conversation_handle    
FROM    dbo.TargetAuditQueue

你知道如何在.net上做同样的事情吗?

[SqlProcedure]
public void AuditParseEventData()
{
    // ???
}

谢谢!

1 个答案:

答案 0 :(得分:2)

    SqlCommand receiveCommand = contextConnection.CreateCommand();
    receiveCommand.Transaction = transaction;
    receiveCommand.CommandText = "RECEIVE TOP(1) message_body, conversation_handle FROM dbo.TargetAuditQueue";
    using (SqlDataReader reader = receiveCommand.ExecuteReader())
    {
        if (reader.Read())
        {
            SqlBinary messageBody = reader.GetSqlBinary(0);
            Guid conversationHandle = reader.GetGuid(1);
            // your stuff...
        }
    }

另请注意,会话句柄与会话ID不同。在你的代码中,你似乎在混合这些。