1

I have to use an existing INSERT trigger which uses CONTEXT_INFO...

How can I pass "context_info" to a c# SqlCommand so that this trigger keeps working correctly?

Usage in SQL-Trigger:

select @ctxt=context_info from master.dbo.sysprocesses where spid = @@spid
set @session=substring(@ctxt,1,4)
set @contextid=substring(@ctxt,5,4)
set @ntduser=substring(@ctxt,9,120)

Tried:

                            //string strContext = "declare @context_info varbinary(30) set @context_info = cast('abcdefghijklmnopqrstuv1234567890' as varbinary(30)) set context_info @context_info";
                            ///////////////

                            SqlCommandBuilder cb = new SqlCommandBuilder(da);
                            da.UpdateCommand = cb.GetUpdateCommand();
                            //da.UpdateCommand.CommandText= da.UpdateCommand.CommandText.Replace(" WHERE ", strContext+" WHERE ");
                            //da.UpdateCommand.CommandText = "SET CONTEXT_INFO  0x1 " + da.UpdateCommand.CommandText;
                            da.Update(dataTable);
                            da.Dispose();

see commented out code... In my SQL Trigger "context_info" was always empty

Already read that: https://social.msdn.microsoft.com/Forums/en-US/4a0ecb28-11cb-45ec-adbd-d72ac65b158a/how-to-pass-net-applications-parameter-to-a-trigger?forum=transactsql but also does not work.

Isn't there a sample out there where context_info is passed to SqlCommand or SqlConnection or SqlTransaction?

5
  • I'm not really following what you're trying to do here. Rather than just modifying and using the UpdateCommand, can you show us what it ends up being? Commented Jun 7, 2018 at 7:51
  • See code sample from my trigger. How can I pass "context_info" ? Commented Jun 7, 2018 at 8:02
  • You are executing a query, not a trigger. If you have such complex logic (why?) write a stored procedure instead of trying to concatenate strings. What are you trying to do in the first place though? If you want to pass user and session information, do so explicitly. Don't try to depend on magic strings and global variables Commented Jun 7, 2018 at 8:16
  • Can't change the trigger since it is not mine. I have use the existing one and this requires context_info for login... Commented Jun 7, 2018 at 8:43
  • Added this info to the question... Commented Jun 7, 2018 at 8:45

1 Answer 1

1

I used something like this (simplified) once to set some information (guid) so that it can be retrieved from inside a trigger ...

string sql = $"DECLARE @session uniqueidentifier; SET @session = '{guid}'; SET CONTEXT_INFO @session";
var cmd = new SqlCommand(sql, connection, transaction);
cmd.ExecuteNonQuery();

Be sure not to close the connection between setting the context_info and invokation of the trigger ...

Sign up to request clarification or add additional context in comments.

1 Comment

I send it before my INSERT command and it works. As described in the link I posted in my question. However, thanks for your help...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.