zoukankan      html  css  js  c++  java
  • 事务的应用

    在同时删除多个表时建议应用事务来处理,防止产生数据不一致。
    public int delete(int id)
      {
       string strConnectionString =Beyondbit.Data.DbConn.Sql_ConnectionString;
     
       int i=0;
       using (SqlConnection cn = new SqlConnection(strConnectionString))
       {
        cn.Open();
        using (SqlTransaction trans = cn.BeginTransaction())
        {
         try
         {
          string strSqlString="delete from userList where id='"+id+"'";
          SqlHelper.ExecuteNonQuery(trans,CommandType.Text, strSqlString);

          strSqlString="delete from taskcenter where id='"+id+"'";
          SqlHelper.ExecuteNonQuery(trans, CommandType.Text, strSqlString);

          strSqlString="delete from form_document where DocID='"+id+"'";
          SqlHelper.ExecuteNonQuery(trans, CommandType.Text, strSqlString);

          trans.Commit();
         }
         catch (Exception ex)
         {
          trans.Rollback();
          i = -1;
          throw ex;
         }
         finally
         {
          cn.Close();
         }
        }
       }
       return i;
      }

  • 相关阅读:
    USACO 2016 February Contest, Gold解题报告
    USACO 2016 January Contest, Gold解题报告
    NOIP2013 Day2
    [DP题]放苹果
    [DP题]登山
    洛谷八连测R6
    [20171025模拟赛]
    [DP题]吃糖果
    [DP题]采药
    spring-security-oauth2 授权服务集成钉钉扫码登录
  • 原文地址:https://www.cnblogs.com/isMe/p/267847.html
Copyright © 2011-2022 走看看