演示者如何知道受DAL方法影响的行

时间:2014-08-10 07:24:55

标签: c# .net sql-server ado.net mvp

我正在使用MVP模式,并且在演示者中处理异常。诸如"Update Successful", "No matching records in DB"等用户消息将由演示者可用的单独UserMassage服务类生成。 (由于消息传递是演示者的责任)

所以我只想知道演示者如何知道DAL方法是更新/删除了行还是失败了?

我目前的方法显示在DataService

中的此示例DAL方法中
    public void DeletePoint(string pid)
    {
        string updateStatement = "IF NOT EXISTS (SELECT PID FROM Attendance WHERE PID = @pid )" + 
                                 "UPDATE Point SET Point.deleted = GETDATE() WHERE PID = @pid";

        using (SqlConnection sqlConnection = new SqlConnection(db.ConnectionString))
        using (SqlCommand sqlCommand = new SqlCommand(updateStatement, sqlConnection))
        {
            sqlCommand.Parameters.Add("@pid", SqlDbType.VarChar).Value = pid;

            sqlConnection.Open();
            var RowsAffected = sqlCommand.ExecuteNonQuery();
            if (RowsAffected > 0)
            {
                throw new Exception("Updated Sucessfully");
            }
            Else
            {
                throw new Exception("Update was not Successful");
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以使用@@ROWCOUNT获取数据库中受影响的行数。然后在DAL方法中将其返回给调用DAL的演示者或服务。