asptransaction.aspx

来自「asp.net技术内幕的书配源码」· ASPX 代码 · 共 49 行

ASPX
49
字号
<%@ Page Language="C#" Transaction="RequiresNew" %>
<%@ Import Namespace="System.EnterpriseServices" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat=server>
void Page_Load(Object sender , EventArgs e) 
{
 SqlConnection conBank;
 string  strUpdateAccountA;
 string  strUpdateAccountB;
 SqlCommand cmdUpdateAccountA;
 SqlCommand cmdUpdateAccountB;

// Initialize objects
conBank = new SqlConnection(@"Server=localhost;Trusted_Connection=true;database=conBank" );

strUpdateAccountA = "Update AccountA set { Balance = Balance - 999.99 WHERE Customer=//Smith//";
cmdUpdateAccountA = new SqlCommand( strUpdateAccountA, conBank );

strUpdateAccountB = "Update AccountB set { Balance = Balance + 999.99 WHERE Customer=//Smith//";
cmdUpdateAccountB = new SqlCommand( strUpdateAccountB, conBank );

// Open connection
conBank.Open();

// try executing both commands
try 
{
  cmdUpdateAccountA.ExecuteNonQuery();
  cmdUpdateAccountB.ExecuteNonQuery();

  // Commit the transaction
  ContextUtil.SetComplete();
  Response.Write( "Transaction Successful!" );
} 
catch (Exception eX) 
{
  ContextUtil.SetAbort();
  Response.Write( "Transaction Failed!" );
}

finally
{
  conBank.Close();
}
}

</script>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?