Viewing source for recipe1013cs.aspx
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
SqlTransaction _Transact;
void page_load(Object Sender, EventArgs E)
{
String ConnectionString = "Server=localhost; Database=pubs; uid=sa; pwd=;";
String CommandText = "UPDATE authors SET state='PU' WHERE state='UT'";
String CommandText2 = "UPDATE authors SET au_id='172-32-1177' WHERE au_id='172-32-1176'";
SqlConnection _Connection = new SqlConnection(ConnectionString);
SqlCommand _Command = new SqlCommand();
try{
// Open connection and begin transaction
_Connection.Open();
_Transact = _Connection.BeginTransaction();
// Set up command and execute statements
_Command.Connection = _Connection;
_Command.Transaction = _Transact;
//Execute the first sql statement
_Command.CommandText = CommandText;
_Command.ExecuteNonQuery();
//Execute the second sql statement
_Command.CommandText = CommandText2;
_Command.ExecuteNonQuery();
// Commit if we got this far
_Transact.Commit();
MessageLiteralControl.Text = "Transaction Successful!";
}
catch (Exception Err){
// Rollback transaction and report error
_Transact.Rollback();
MessageLiteralControl.Text = "Transaction Failed!<br/>" + Err.Message;
}
finally{
// Clean up resources
_Connection.Close();
}
}
</script>
<html>
<head>
<title>Recipe1013</title>
</head>
<body style="FONT-FAMILY: arial">
<form runat="server" ID="Form1">
<p>
<asp:Label id="MessageLabel" runat="server" EnableViewState="false"></asp:Label>
</p>
</form>
</body>
</html>