I'm trying to set transaction isolation level in Dapper using SqlBuilder
, but didn't seem to work.
var builder = new SqlBuilder();
var sqlStatement = builder.AddTemplate(@"
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT * FROM Users
SET TRANSACTION ISOLATION LEVEL READ COMMITTED");
conn.Query<User>(sqlStatement.RawSql);
But this works:
conn.Execute("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED");
var result = conn.Query<User>(sqlStatement);
conn.Execute("SET TRANSACTION ISOLATION LEVEL READ COMMITTED");
I figured this maybe because with SqlBuilder
, Dapper builds a dynamic SQL and execute with sp_executesql
stored procedure.
To prove my hypothesis, I did try the following SQL statements:
exec sp_executesql N'SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED'
After that statement, I queried sys.dm_exec_sessions
to check my connection and it's still showing ReadCommitted
which is the default isolation level in my database.
Any way to get around setting transaction level (or any other SET
statement) in a separate .Execute
? Or maybe a special way to use SET
statement in sp_executesql
?