在Integration Test C#中返回更新的行

时间:2013-11-19 21:44:56

标签: c# sql sql-server stored-procedures

在我的代码中,我最初有一个通过调用存储过程来禁用行的测试方法。如果值从数据库中的先前值更改,则我的存储过程会更新disable列,否则我会定期更新,常规更新会排除禁用列的更新。

IF(@ThingID IS NOT NULL)    
BEGIN      
    SELECT @StoredDisable = Disable FROM Things WHERE ThingID = @ThingID        
    --If the Disable value is different then we are doing   
    IF(@Disable != @StoredDisable)
        BEGIN
            UPDATE Things 
                SET Disable=@Disable
                WHERE ThingID = @ThingID            
            SET @ResultMessage = 'Successfully Deleted'
            SELECT @ResultMessage AS 'Message'                
            RETURN 0
            END     
        END       
    ELSE --If the Disable value is the same then we are just updating the fields  
    BEGIN
            UPDATE Things               
            SET thing1=ISNULL(@thing1, thing1),thing2=ISNULL(@thing1, thing2).....  
                WHERE thingID = @thing1ID                                                                   
            SET @ResultMessage = 'Successfully Updated'
            SELECT @ResultMessage AS 'Message'                                          
            RETURN 0                
    END     
END

我已经在我的SQL Server管理器中对此进行了测试,它运行得非常好。但是当我在测试中运行它时,它不会禁用该行,而是更新行。我第一次在我的数据库中的一个预先存在的行上执行它,但后来在我尝试禁用它之前插入了我自己的行

command.Parameters["@ThingID"].Value = DBNull.Value;
command.Parameters["@Comment"].Value = "Inserted from test";

command.ExecuteNonQuery();

 using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
 {
--Insert a new row and got the ordinal and the value of Disable (store in atemp)
--and the thingId (store in result.Id)
Console.WriteLine("the row Disable: {0}",     
    atemp);                                   
 }
  -----------Insert Done----------

 Console.WriteLine("the row id: {0}", result.Id);

 command.Parameters["@ThingID"].Value = result.Id;
 //command.Parameters["@Comment"].Value = "Disable from test";
 command.Parameters["@Disable"].Value = 1;

 connection.Open();
 command.ExecuteNonQuery();

  using (var reader = command.ExecuteReader(CommandBehavior.CloseConnection))
  {
var tempDisable = false;
var DisableOrdinal = reader.GetOrdinal("Disable");
while (reader.Read())
{
    tempDisable = reader.GetBoolean(DisableOrdinal);
}
Console.WriteLine("the Disable before: {0}", tempDisable);
reader.NextResult();

reader.NextResult();
while (reader.Read())
{
    tempDisable = reader.GetBoolean(DisableOrdinal);
}
Console.WriteLine("the Disable After: {0}", tempDisable);   
 }                      
 ... the rest of code

我所做的是在我的插入中添加了一个select语句,以便我可以获得结果集adn,看到Disable的值,如预期的那样是假的。然后我在禁用/更新存储过程中添加了一个select语句,然后执行任何操作以获取结果集以查看禁用是什么,这是真的,我还没有做任何事情,因此它调用更新部分不更新禁用列。所以我不知道发生了什么。下面是我的测试输出控制台

the row Disable : False
the row id: 1898
the Disable before: True
the Disable After: True  

1 个答案:

答案 0 :(得分:0)

它必须被调用两次: 1)你禁用了它 2)因为@Disable参数== 1它与现有值相同所以您更新值

为什么它被调用两次是另一个问题 - 也许是一些愚蠢的事情 - 比如执行两次测试或者一些调试器Watch执行命令以获得值观察(听起来很有趣但这就是我曾经发生的事情;)