Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using System;
using System.Data;
using System.Data.SqlClient;
using System.Transactions;
using IsolationLevel = System.Transactions.IsolationLevel;
namespace ConsoleApplication15
{
class Program
{
static void Main(string[] args)
{
LogTrasactionInfo(1);
using (var scope = new TransactionScope(TransactionScopeOption.Required,new TransactionOptions() {IsolationLevel = IsolationLevel.ReadCommitted}))
{
var connection = new SqlConnection("Data Source=.;Initial Catalog=ServicesPortal;Integrated Security=True");
LogTrasactionInfo(2);
ExecuteSelect(connection);
LogTrasactionInfo(3);
using (var scope1 = new TransactionScope(TransactionScopeOption.RequiresNew, new TransactionOptions() {IsolationLevel = IsolationLevel.Serializable}))
{
LogTrasactionInfo(4);
ExecuteSelect(connection);
scope1.Complete();
}
LogTrasactionInfo(5);
scope.Complete();
}
LogTrasactionInfo(6);
Console.ReadLine();
}
private static void LogTrasactionInfo(int step)
{
var current = Transaction.Current;
if (current != null) Console.WriteLine("{1} Level: {0}", current.IsolationLevel, step);
else Console.WriteLine("{0} No Tx", step);
}
private static int ExecuteSelect(SqlConnection connection)
{
{
if (connection.State != ConnectionState.Open ) connection.Open();
var command = new SqlCommand("Select Count(*) From dbo.Users", connection);
var result = (int)command.ExecuteScalar();
return result;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment