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(); } }
The isolation level of the transactional support is default to READ UNCOMMITTED. You can change it to READ COMMITTED SNAPSHOT ISOLATION by turning ON the READ_COMMITTED_SNAPSHOT database option for a user database when connected to the master database.
The default isolation level is REPEATABLE READ . Other permitted values are READ COMMITTED , READ UNCOMMITTED , and SERIALIZABLE .
The Serializable isolation level provides the strictest transaction isolation. This level emulates serial transaction execution for all committed transactions; as if transactions had been executed one after another, serially, rather than concurrently.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With