Exemple #1
0
        }          // end ExecuteNonQueryWithinTransactionScope

        /// <summary>
        /// Connect to two databases while manually enlisting in the ambient Transaction
        /// by appending ";enlist=false" to the connection strings and issuing EnlistTransaction().
        /// Execute two non-query SQL commands under the TransactionScope.
        /// Commit the Transaction by marking the TransactionScope complete,
        /// if issueScopeComplete argument is set true for testing purposes.
        /// </summary>
        /// <param name="connString1">ConnectionString for database 1.</param>
        /// <param name="connString2">ConnectionString for database 2.</param>
        /// <param name="commandText1">Non-query CommandText to execute against database 1.</param>
        /// <param name="commandText2">Non-query CommandText to execute against database 2.</param>
        /// <param name="issueScopeComplete">If true, issue TransactionScope.Complete()</param>
        static void ExecuteNonQueryWithinExplicitTransactionScope(
            string connString1, string connString2,
            string commandText1, string commandText2,
            bool issueScopeComplete)
        {
            Console.WriteLine("\n\tUpdateUsingTransactionScope...\n");

            using (TransactionScope scope = new TransactionScope())
            {
                using (IngresConnection conn1 =
                           new IngresConnection(connString1 + ";enlist=false"))
                {
                    using (IngresConnection conn2 =
                               new IngresConnection(connString2 + ";enlist=false"))
                    {
                        Console.WriteLine("\tIngresConnection1.Open()...");
                        // Open the connection to the database and
                        // enlist in the ambient Transaction using MSDTC
                        conn1.Open();
                        Console.WriteLine("\tIngresConnection1.Open() complete\n");

                        Console.WriteLine("\tIngresConnection2.Open()...");
                        // Open the connection to the database and
                        // enlist in the ambient Transaction using MSDTC
                        conn2.Open();
                        Console.WriteLine("\tIngresConnection2.Open() complete\n");

                        Console.WriteLine("\tManually enlist in Transaction.Current...");
                        // manually enlist in the current ambient transaction;
                        Transaction tx = Transaction.Current;
                        conn1.EnlistTransaction(tx);
                        conn2.EnlistTransaction(tx);
                        Console.WriteLine("\tEnlisted.\n");

                        try
                        {
                            IngresCommand cmd1 = conn1.CreateCommand();
                            cmd1.CommandText = commandText1;
                            cmd1.ExecuteNonQuery();

                            IngresCommand cmd2 = conn2.CreateCommand();
                            cmd2.CommandText = commandText2;
                            cmd2.ExecuteNonQuery();

                            // mark the Transaction complete
                            if (issueScopeComplete)                              // test debug flag for testing scope.Complete
                            {
                                Console.WriteLine("\tTransactionScope completing...");
                                scope.Complete();
                                Console.WriteLine("\tTransactionScope complete\n");
                            }
                            // note: TransactionScope will not be committed until
                            // TransactionScope Dispose() is called.
                        }
                        catch (Exception ex)
                        {
                            string s = ex.ToString();
                            Console.WriteLine("\n\tApplication throws Exception!!  " + s + "\n");
                            throw;
                        }
                    }              // end using (IngresConnection2)  // closes and disposes conn2
                }                  // end using (IngresConnection1)      // closes and disposes conn1

                Console.WriteLine("\tTransactionScope.Dispose()...");
            }      // end using (TransactionScope)  // calls System.Transactions.Dispose() --> System.Transactions.CommittableTransaction.Commit
            Console.WriteLine("\tTransactionScope.Dispose() complete\n");
        }          // end ExecuteNonQueryWithinExplicitTransactionScope