Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- testEntities te = (testEntities)sender;
- DbConnection dc = te.Connection;
- DbCommand dcc = dc.CreateCommand();
- dcc.CommandType = CommandType.StoredProcedure;
- DbParameter dp = new EntityParameter();
- dp.ParameterName = "userid";
- dp.Value = textBox1.Text;
- dcc.CommandText = "userinit";
- dcc.Parameters.Add(dp);
- dcc.ExecuteNonQuery();
- StringBuilder cStr = new StringBuilder("declare @tx char(50); set @tx='");
- cStr.Append(textBox1.Text);
- cStr.Append("'; declare @m binary(128); set @m = cast(@tx as binary(128)); set context_info @m;");
- testEntities te = (testEntities)sender;
- DbConnection dc = te.Connection;
- DbCommand dcc = dc.CreateCommand();
- dcc.CommandType = CommandType.Text;
- dcc.CommandText = cStr.ToString();
- dcc.ExecuteNonQuery();
- -- This is for the data
- create table datastuff (
- id int not null identity(1, 1),
- userid nvarchar(64) not null default(''),
- primary key(id)
- )
- go
- -- This is for the log
- create table naplo (
- id int not null identity(1, 1),
- userid nvarchar(64) not null default(''),
- datum datetime not null default('2099-12-31'),
- primary key(id)
- )
- go
- create trigger myTrigger on datastuff for insert as
- declare @User_id int,
- @User_context varbinary(128),
- @User_id_temp varchar(64)
- select @User_context = context_info
- from master.dbo.sysprocesses
- where spid=@@spid
- set @User_id_temp = cast(@User_context as varchar(64))
- declare @insuserid nvarchar(64)
- select @insuserid=userid from inserted
- insert into naplo(userid, datum)
- values(@User_id_temp, getdate())
- go
- create procedure userinit(@userid varchar(64))
- as
- begin
- declare @m binary(128)
- set @m = cast(@userid as binary(128))
- set context_info @m
- end
- go
- using (testEntities te = new testEntities())
- {
- // Hook to the event
- te.SavingChanges += new EventHandler(te_SavingChanges);
- // This is important, because the context info is set inside a connection
- te.Connection.Open();
- // Add a new datastuff
- datastuff ds = new datastuff();
- // This is coming from a text box of my test form
- ds.userid = textBox1.Text;
- te.AddTodatastuff(ds);
- // Save the changes
- te.SaveChanges(true);
- // This is not needed, only to make sure
- te.Connection.Close();
- }
- // Take my entity
- testEntities te = (testEntities)sender;
- // Get it's connection
- EntityConnection dc = (EntityConnection )te.Connection;
- // This is important!
- DbConnection storeConnection = dc.StoreConnection;
- // Create our command, which will call the userinit SP
- DbCommand command = storeConnection.CreateCommand();
- command.CommandText = "userinit";
- command.CommandType = CommandType.StoredProcedure;
- // Put the user id as the parameter
- command.Parameters.Add(new SqlParameter("userid", textBox1.Text));
- // Execute the command
- command.ExecuteNonQuery();
- private int _contextUserId;
- public void SomeMethod()
- {
- var db = new MyEntities();
- db.Connection.StateChange += this.Connection_StateChange;
- this._contextUserId = theCurrentUserId;
- // whatever else you want to do
- }
- private void Connection_StateChange(object sender, StateChangeEventArgs e)
- {
- // only do this when we first open the connection
- if (e.OriginalState == ConnectionState.Open ||
- e.CurrentState != ConnectionState.Open)
- return;
- // use the existing open connection to set the context info
- var connection = ((EntityConnection) sender).StoreConnection;
- var command = connection.CreateCommand();
- command.CommandText = "proc_ContextInfoSet";
- command.CommandType = CommandType.StoredProcedure;
- command.Parameters.Add(new SqlParameter("ContextUserID", this._contextUserId));
- command.ExecuteNonQuery();
- }
Add Comment
Please, Sign In to add comment