zoukankan      html  css  js  c++  java
  • SQL Server事务处理

     1         /// <summary>
     2         /// SQL Server事务处理
     3         /// </summary>
     4         /// <param name="sender"></param>
     5         /// <param name="e"></param>
     6         private void btn_Tran_click(object sender, EventArgs e)
     7         {
     8             //------------------------------------------------------------//
     9 
    10             SqlConnection con = new SqlConnection(server = (local); database = FreeNotes; UID = hf; Pwd = 123);
    11 
    12             //------------------------------------------------------------//
    13             
    14             //*** 要转钱的银行账户
    15 
    16             //用来获取登录卡号的账户余额
    17             int u1_money = GetMoney(Login.AccountNum.Tostring());
    18 
    19             //用来获取要转钱的金额
    20             int money = Int32.Parse(this.txtMonrey.Text);
    21 
    22             //此时获得,减去转出金额之后的AccountNum账户的余额。
    23             int ul_balance = u1_money - money;
    24 
    25             //如果要转的金额大于卡里的金额,则提示余额不足。
    26             if (ul_balance < 0)
    27             {
    28                 MessageBox.Show("账户余额不足!", "提示");
    29             }
    30             //------------------------------------------------------------//
    31 
    32             //*** 接收钱的银行账户
    33 
    34             //获得要转入txtInAccountmoney账户的余额信息
    35             int u2_money = GetMoney(txtInAccountmoney.Text.Trim());
    36 
    37             //此时获得的是另一个账户的余额加上即将转入金额的总计。
    38             int u2_balance = u2_money + money;
    39 
    40 
    41             //------------------------------------------------------------//
    42 
    43             //SQL语句
    44 
    45             //转出钱的SQL语句
    46             string sqlIn = "";
    47 
    48             //转入钱的SQL语句
    49             string sqlOut = "";
    50 
    51             //打开数据库连接
    52             con.Open();
    53 
    54             //创建事务对象
    55             SqlTransaction st = con.BeginTransaction();
    56 
    57             //创建账户1的SQLCommand对象
    58             SqlCommand cmd1 = new SqlCommand(sqlIn, con);
    59 
    60             //创建账户2的SQLCommand对象
    61             SqlCommand cmd2 = new SqlCommand(sqlOut, con);
    62 
    63             //把对象1添加到事务对象st中
    64             cmd1.Transaction = st;
    65 
    66             //把对象2添加到事务对象st中
    67             cmd2.Transaction = st;
    68 
    69             try
    70             {
    71                 //返回执行的行数,以为使用的两个SQL语句,所以是二。
    72                 int i = cmd1.ExecuteNonQuery() + cmd2.ExecuteNonQuery();
    73 
    74                 //判断是不是等于2,等于二就执行成功。
    75                 if(i != 2)
    76                 {
    77                     //不等于2,抛出异常!
    78                     throw new Exception();
    79                 }
    80                 else
    81                 {
    82                     MessageBox.Show("转账成功!", "提示");
    83                 }
    84             }
    85             catch(Exception ex)
    86             {
    87                 st.Rollback();
    88                 MessageBox.Show("转账失败!", "提示");
    89             }
    90             finally
    91             {
    92                 con.Close();
    93             }
    94         }
  • 相关阅读:
    [LeetCode]Add Two Numbers
    [LeetCode]Longest SubString Without Repeating Characters
    [LeetCode]Median of Two Sorted Arrays
    [LeetCode]Two Sum
    动态规划
    [shell编程]一个简单的脚本
    一些linux的问题
    核稀疏表示分类(KSRC)
    conda 按照指定源下载python包
    python 保留两位小数
  • 原文地址:https://www.cnblogs.com/KTblog/p/4448471.html
Copyright © 2011-2022 走看看