begin tran 
select * from table1 
if @@error=0 
commit 
else 
rollback 

解决方案 »

  1.   

    SQLServer2005中可以使用try catch
      

  2.   

    public int addUser(UserDTO user)
        {
            int row = 0;
            try
            {
                sqlCom = new SqlCommand("insert into users values(@name,@pwd,@sex,@email,@picture,@phone)",sqlCon);
                SqlParameter[] param = { 
                                            new SqlParameter("@name",SqlDbType.VarChar),
                                            new SqlParameter("@pwd",SqlDbType.VarChar),
                                            new SqlParameter("@sex",SqlDbType.VarChar),
                                            new SqlParameter("@email",SqlDbType.VarChar),
                                            new SqlParameter("@picture",SqlDbType.VarChar),
                                            new SqlParameter("@phone",SqlDbType.VarChar)
                                       };
                param[0].Value = user.Name;
                param[1].Value = user.Pwd;
                param[2].Value = user.Sex;
                param[3].Value = user.Email;
                param[4].Value = user.Picture;
                param[5].Value = user.Phone;            row = sqlCom.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return row;
        }改成事务可以吗