using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;

namespace ClientFailover
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SqlConnection conn = new SqlConnection(@"server=SQLLaunchVPC\SQLDev01;failover partner=SQLLaunchVPC\SQLDev02;integrated security=sspi;database=test"))
            using (SqlCommand cmd = new SqlCommand())
            {
                conn.Open();
                SqlTransaction tx = null;
                for (int i = 1; i < 101; i++)
                {
                    try
                    {
                        // failover will close the connection, you have to reopen it 
                        // or put the conn.Open here and remove it from before the tx = null line
                        if (conn.State == System.Data.ConnectionState.Closed)
                            conn.Open();

                        cmd.Connection = conn;
                        tx = conn.BeginTransaction();
                        cmd.Transaction = tx;
                        cmd.CommandText = "insert jobs values('row " + i + "',10,10)";
                        int numrows = cmd.ExecuteNonQuery();
                        // stop at this line
                        Console.WriteLine("row {0} added", i);
                        tx.Commit();
                        Console.WriteLine("transaction {0} committed", i);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e.Message);
                        try
                        {
                            // this has to be in a try-catch block with no catch
                            // because losing the master will cause a transaction
                            // that cannot be committed on rolled back
                            tx.Rollback();
                        }
                        catch
                        { }
                    }
                    finally
                    {
                    }
                }
                // SqlCommand and SqlConnection disposed here
            }

        }
    }
}
