31

The following method is supposed to peroform a dirty read on an open connection. There are no transactions. Where do I set IsolationLevel?

public string DoDirtyRead(string storedProcName, SqlConnection connection) { using (SqlCommand command = new SqlCommand(storedProcName, connection)) { command.CommandType = CommandType.StoredProcedure; // HOW TO SET IsolationLevel to READ_UNCOMMITTED here? command.ExecuteNonQuery(); } } 

5 Answers 5

29

If you don't want to do transactions, you can set it once when you open the connection and it will remain at that setting until you change it. So just do:

connection.BeginTransaction(IsolationLevel.ReadUncommitted).Commit(); 

Probably not the best for your specific case, since you are opening the connection, using it, and throwing it away, but I wanted to put this answer in for anyone with a longer-lived connection.

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

3 Comments

BeginTransaction returns a DbTransaction which is IDisposable. Where do you call Dispose on it if you discard the reference?
If you want to avoid the disposable return object another option is to just execute a non-query of SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; after you open your connection. Then the connection will be in that state until it is closed or altered otherwise.
Watch out for connection pool... When you create your next "new" connection from the same connectionstring, it gives you back your old connection, with the same isolation level that you may have set.
13

On the BeginTransaction method: (MSDN link)

And if you just want to use hints in your SP at the table level, use WITH(NOLOCK) - but use at your own risk.

3 Comments

(So you'd need to wrap this in a transaction to set isolation level, or just use hints in your SP).
I dont want to use a transaction at all
ok, have to do the transactions then and pass read_uncommitted. Thanks!
9

Given you already have an existing connection (and possibly an existing transaction), I'd use a TransactionScope to control the isolation level of the child. This does a dirty read rowcount (I believe):

using (var command = connection.CreateCommand()) using(new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions{IsolationLevel = IsolationLevel.ReadUncommitted})) { command.CommandText = string.Format("select count(*) from {0}", tableName); return (int)command.ExecuteScalar(); } 

1 Comment

How do I set the IsolationLevel in .NET5+?
8

In your Stored Procedure, for transact-sql use:

SET TRANSACTION ISOLATION LEVEL read uncommitted -- 0 SET TRANSACTION ISOLATION LEVEL read committed -- 1 SET TRANSACTION ISOLATION LEVEL repeatable read -- 2 SET TRANSACTION ISOLATION LEVEL read serializable -- 3 

2 Comments

Also, consider setting the DATAROWS Lock Scheme for your tables.
thanks for your answer. This won't help because it will make the stored procedure do dirty reads all the time. Instead i want to control it in my code with the transaction or, if possible, without a transaction, so the stored proc can be called with either level of isolation.
0

Add another parameter to your stored procedure to indicate the isolation level you want the stored procedure to run with.

IF @isolevel = 0 SET TRANSACTION ISOLATION LEVEL read uncommitted; ELSE

Also I believe uncommitted needs two "t's" in it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.