the use of a transaction : SqlTransaction « ADO.Net « C# / CSharp Tutorial






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

class ExecuteTransaction {
    public static void Main() {
        SqlConnection mySqlConnection = new SqlConnection("server=localhost;database=Northwind;uid=sa;pwd=sa");

        mySqlConnection.Open();
        SqlTransaction mySqlTransaction = mySqlConnection.BeginTransaction();
        SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
        mySqlCommand.Transaction = mySqlTransaction;
        mySqlCommand.CommandText =
          "INSERT INTO Customers (" +
          "  CustomerID, CompanyName" +
          ") VALUES (" +
          "  'J3COM', 'Jason Price Corporation'" +
          ")";
        mySqlCommand.ExecuteNonQuery();
        mySqlCommand.CommandText =
          "INSERT INTO Orders (" +
          "  CustomerID" +
          ") VALUES (" +
          "  'J3COM'" +
          ")";
        mySqlCommand.ExecuteNonQuery();
        mySqlTransaction.Commit();
        mySqlConnection.Close();
    }
}








32.28.SqlTransaction
32.28.1.Updating Data Using Transactions
32.28.2.the use of a transaction
32.28.3.Nested Implicit Transaction
32.28.4.Implicit Transaction
32.28.5.Explicit Transaction