using System;
using System.Data;
using System.Data.SqlClient;

namespace Snapshot
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class Class1
	{
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
      DoSnapshot();
		}

		static public void DoSnapshot()
		{
      string connstr = GetConnectStringFromConfigFile();
			SqlConnection conn = new SqlConnection(connstr);
			conn.Open();
			SqlCommand myCommand = conn.CreateCommand();
			SqlTransaction tx = null;
			// Start a local transaction.
			tx = conn.BeginTransaction(IsolationLevel.Snapshot);
			// Associate Command with Transaction
			SqlCommand cmd = new SqlCommand(
			"insert into jobs values('new job', 100, 100)",conn, tx);
			
			try 
			{
				// first insert
				cmd.ExecuteNonQuery();
				// second insert
				cmd.CommandText =
				"insert into jobs values('Grand Poobah', 200, 200)";
				cmd.ExecuteNonQuery();
				tx.Commit();
			}
			catch(Exception e) 
			{
				try 
				{
					tx.Rollback();
				}
				catch (SqlException ex) {
				// no-op catch, tx already rolled back
				}
			}
			finally 
			{
				cmd.Dispose();
				conn.Dispose();
			}
		}

    static string GetConnectStringFromConfigFile()
    {
      return "server=.;integrated security=true;database=pubs";
    }
	}
}
