1
<%@ Page Language="C#"%>
2
<%@ Import Namespace="System.Data" %>
3
<%@ Import Namespace="System.Data.SqlClient" %>
4
5
<script runat=server>
6
7
void Page_Load(Object sender , EventArgs e)
8

{
9
SqlTransaction objTransaction;
10
SqlConnection conBank;
11
string strUpdateAccountA;
12
string strUpdateAccountB;
13
SqlCommand cmdUpdateAccountA;
14
SqlCommand cmdUpdateAccountB;
15
16
// Initialize objects
17
conBank = new SqlConnection( "Server=localhost;Integrated Security=SSPI;database=Bank" );
18
19
strUpdateAccountA = "Update AccountA set { Balance = Balance - 999.99 WHERE Customer=//Smith//";
20
cmdUpdateAccountA = new SqlCommand( strUpdateAccountA, conBank );
21
22
strUpdateAccountB = "Update AccountB set { Balance = Balance + 999.99 WHERE Customer=//Smith//";
23
cmdUpdateAccountB = new SqlCommand( strUpdateAccountB, conBank );
24
25
// Open connection
26
conBank.Open();
27
28
// Retrieve transaction from connection
29
objTransaction = conBank.BeginTransaction();
30
31
// Assign Transaction to commands
32
cmdUpdateAccountA.Transaction = objTransaction;
33
cmdUpdateAccountB.Transaction = objTransaction;
34
35
// try { executing both commands
36
try
{
37
cmdUpdateAccountA.ExecuteNonQuery();
38
cmdUpdateAccountB.ExecuteNonQuery();
39
40
// Commit the transaction
41
objTransaction.Commit();
42
Response.Write( "Transaction Successful!" );
43
}
44
catch (Exception eX)
45
{
46
objTransaction.Rollback();
47
Response.Write( "Transaction Failed!" );
48
}
49
finally
50
{
51
conBank.Close();
52
}
53
}
54
</script>
55

2

3

4

5

6

7

8



9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36



37

38

39

40

41

42

43

44

45



46

47

48

49

50



51

52

53

54

55
