/// <summary>
     /// 执行事务,返回0或者1
     /// </summary>
     /// <param name="Sqlstr">sql语句</param>
     /// <returns></returns>
    public int ExecTran(string[] Sqlstr)
     {
         SqlConnection conn = Connection;
        SqlTransaction tran = conn.BeginTransaction();//先实例SqlTransaction类,使用这个事务使用的是con 这个连接,使用BeginTransaction这个方法来开始执行这个事务
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.Transaction = tran;
       try{
          int count = Sqlstr.Length;
          for (int i = 0; i < count; i++)
          {
              cmd.CommandText = Sqlstr[i];
              cmd.ExecuteNonQuery();
          }
          tran.Commit();
          return 1;
       }
      catch{
         tran.Rollback();
         return 0;
       }
      finally
       {
        CloseDB();
        tran.Dispose();
       }
     }

posted on 2013-12-09 15:40  踏雪寻梦  阅读(567)  评论(0编辑  收藏  举报