Private Sub UpdateWithTransactions()
Dim conn As SqlConnection = New SqlConnection
conn.ConnectionString = "server=NOMEDBSERVER;database=NOMEDB;uid=USERID;pwd=PASSWORD;"
conn.Open()
Dim trans As SqlTransaction
trans = conn.BeginTransaction()
Try
Dim sql1 As String = "Insert INTO Tabella1 (campo) VALUES ('valore')"
Dim sql2 As String = "Insert INTO Tabella2 (campo1,campo2) VALUES ('valore1','valore2')"
Dim comm As SqlCommand = New SqlCommand
comm.Connection = conn
'Inizia la transazione
comm.Transaction = trans
'prima query
comm.CommandText = sql1
comm.ExecuteNonQuery()
'seconda query
comm.CommandText = sql2
comm.ExecuteNonQuery()
'eseguo la transazione
trans.Commit()
Response.Write("Fine elaborazione")
Catch ex As SqlException
'se si sono verificati errori faccio il rollback
trans.Rollback()
Response.Write(ex.ToString())
Finally
'chiudiamo sempre le connessioni!
conn.Close()
End Try
End Sub