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;
      }

  • 相关阅读:
    03 类与对象
    课堂作业02程序设计
    动手动脑
    做课题时的一些问题
    课题一
    《大道至简》JAVA伪代码读后感
    题目-兔子繁衍问题
    题目-求一个日期是该年中的第几天
    题目-查验身份证
    题目-删除重复字符
  • 原文地址:https://www.cnblogs.com/isMe/p/267847.html
Copyright © 2011-2022 走看看