How to set isolation level on SqlCommand/SqlConnection initialized with no transaction

前端 未结 5 1512
忘了有多久
忘了有多久 2020-12-03 10:18

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 DoDi         


        
5条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-03 10:52

    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();
    }
    

提交回复
热议问题