Diego Jancic
Diego Jancic

Reputation: 7440

Receive Service Broker from a CLR stored procedure

I'm trying to move this T-SQL stored procedure to a CLR procedure, but there's a Service Broker specific command that I don't know how to implement:

DECLARE @msgBody XML    
DECLARE @dlgId uniqueidentifier

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

Do you know how to the same thing on .net?

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

Thanks!

Upvotes: 2

Views: 685

Answers (1)

Pawel Marciniak
Pawel Marciniak

Reputation: 2228

    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...
        }
    }

Also, note that conversation handle is something different than conversation ID. In your code you seem to be mixing these.

Upvotes: 2

Related Questions