Skip to content

Instantly share code, notes, and snippets.

@pawelpabich
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpabich/6cb1acd9369ba810ba81 to your computer and use it in GitHub Desktop.
Save pawelpabich/6cb1acd9369ba810ba81 to your computer and use it in GitHub Desktop.
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