C# linq-to-sql, set a database session variable in every session -


question: there way set database session variable:

declare @app_user varchar(30)='otto' 

using linq-to-sql in c#, such variable available triggers in db?

background: i'm implementing poor-man's journaling using triggers, , want pass application user inclusion in journal table:

create trigger [trg_jrn_table1] on [dbo].[table1] insert, update, delete ... insert dbo.[jrn_table1] ([jrndmltype],[jrnuser],<cols>...) select @dml_type, @app_user, <cols> inserted (nolock) 

(i know i'll need bit suser_sname track ad hoc changes, too)

everything can find linq2sql , session variables focused on c# session variables, , don't know start. hoping like:

datacontext dc = createdatacontext(cfg); dc.addsessiondata("app_user", "otto"); 

but naturally doesn't exist :-)

is db session variable possible? or maybe using :setvar or context_info?

update 4/22/2016: i've tried adding c# code in 2 places, datacontext created , right before update. can see command being executed in log, within triggers, temporary data unavailable.

  1. setting session variable:

    datacontext.executecommand("declare @app_user varchar(30)={0}", username); 
  2. in temp table

    datacontext.executecommand("select {0} usrname #session_info", username); 
  3. using context_info (see: link)

    datacontext.executecommand("declare @length tinyint;declare @ctx varbinary(128);select @length = len({0});select @ctx = convert(binary(1), @length) + convert(varbinary(127), {1});set context_info @ctx", username, username); 


Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -