Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on May 12th, 2012  |  syntax: C#  |  size: 1.22 KB  |  views: 78  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Data;
  3. using Npgsql;
  4. using System.Transactions;
  5.  
  6. public class TransactionExample
  7. {
  8.  
  9.     public static void Main(String[] args)
  10.     {
  11.  
  12.             string connectionString = "Server=127.0.0.1;User id=npgsql_tests;password=npgsql_tests;Enlist=true";
  13.             using (TransactionScope tx = new TransactionScope())
  14.             {
  15.                 using (NpgsqlConnection connection = new
  16. NpgsqlConnection(connectionString))
  17.                 {
  18.                     connection.Open();
  19.                     using (NpgsqlCommand command = new
  20. NpgsqlCommand("insert into tablea (cola) values ('b')", connection))
  21.                     {
  22.                         command.ExecuteNonQuery();
  23.                     }
  24.                     using (NpgsqlConnection connection2 = new
  25. NpgsqlConnection(connectionString))
  26.                     {
  27.                         connection2.Open();
  28.                         using (NpgsqlCommand command = new
  29. NpgsqlCommand("insert into tablea (colb) values ('c')", connection2))
  30.                         {
  31.                             command.ExecuteNonQuery();
  32.                         }
  33.                     }
  34.                 }
  35.                 tx.Complete();
  36.             }
  37.     }
  38. }