I execute several lengthy SQL queries as part of the reporting module. These queries are built dynamically at runtime. Depending on user input, they can be single or multi-tasking, have one or more parameters and work with one or more database tables - in other words, their form cannot be easily expected.
Currently, I just follow these instructions in a regular SqlConnection , i.e.
using (SqlConnection cn = new SqlConnection(ConnectionString)) { cn.Open(); // command 1 // command 2 // ... // command N }
Since these queries (actually query packets) may take some time, I am worried about table locks that support reading / writing for other users. This is not a problem if the data for these reports changes during package execution; report queries should never take precedence over other operations on these tables and should not block them.
For most of the lengthy / multitasking operations involved in changing data, I will use transactions. The difference here is that these report requests do not modify any data. Will I be right to wrap these report requests in SqlTransaction in order to control the isolation level?
i.e:
using (SqlConnection cn = new SqlConnection(ConnectionString)) { cn.Open(); using (SqlTransaction tr = cn.BeginTransaction(IsolationLevel.ReadUncommitted)) {
Will my desired result be achieved? Is it right to make a transaction, even if the data has not been changed? Is there any other approach?
Bradley smith
source share