Execute As SQL Command and Linq to SQL

I am trying to execute a sql query as another input using the Run As command. I use Linq for SQL, so I created the Data Context class, and I use the ExecuteQuery method to run the SQL Execute As command. Then I invoke the Linq to SQL command, which succeeds. However, each subsequent request fails with the following error:

A serious error occurred in the current team. Results, if any, should be discarded.

Here is the code snippet I tried:

SummaryDataContext summary = new SummaryDataContext();
summary.ExecuteQuery<CustomPostResult>(@"Execute as Login='Titan\Administrator'");
var test = summary.Customers.First();
var test2 = summary.Products.ToList();

No matter what request I run in the second request, I get an error message at the top. Any help would be greatly appreciated.

+2
3

, ADO.NET.

SqlCommand cmd = new SqlCommand("EXECUTE AS USER = 'operator'");
cmd.Connection = dc.Connection as SqlConnection;
cmd.Connection.Open();
cmd.ExecuteNonQuery();

// do the rest of the queries using linq to sql
+2

, , - .

, DataContext.Connection.ConnectionString. OnCreated(), . , , :

YourDataContext dc = new YourDataContext();
dc.Connection.ConnectionString = "connection string here";

http://www.mha.dk/post/Setting-DataContext-Connection-String-at-runtime.aspx

0

, , ruskey, Execute as User, , . - . , , .

 SqlCommand cmd = new SqlCommand("EXECUTE AS USER = 'domain\\user';");
 OSSDBDataContext dc = new OSSDBDataContext();
 cmd.Connection = dc.Connection as SqlConnection;
 cmd.Connection.Open();
 cmd.ExecuteNonQuery();

 //Execute stored procedure code goes here

 SqlCommand cmd2 = new SqlCommand("REVERT;");
 cmd2.Connection = dc.Connection as SqlConnection;
 cmd2.ExecuteNonQuery();
0
source

Source: https://habr.com/ru/post/1756987/


All Articles