2011-09-19 6 views
2

Enterprise Library 5.0의 예제를 통해 실행 중이고 ExecuteNonQuery를 사용하여 sproc 업데이트를 실행하면 2가 반환됩니다. 업데이트는 ProductID 인 테이블의 기본 키 (예 , 나는 검사했다. 그리고 그것은 유일하다). SSMS이 SP가 질의 결과 창 오른쪽 하단에 "1 행"도시 실행ExecuteNonQuery가 1 레코드 만 업데이트되었을 때 값 2를 반환합니다.

ALTER PROCEDURE [dbo].[UpdateProductsTable] 
    @ProductID int, 
    @ProductName varchar(50) = NULL, 
    @CategoryID int = NULL, 
    @UnitPrice money = NULL 
AS 
BEGIN 
    UPDATE Products 
    SET 
     ProductName = @ProductName, 
     CategoryID = @CategoryID, 
     UnitPrice = @UnitPrice 
    WHERE ProductID = @ProductID 
END 

및 그리드 반환 값 0 :

여기서 단순 SPROC이다. 메시지 탭을 클릭하면 내가 여기이 3 회보고 있어요 왜

(1 row(s) affected) 

(1 row(s) affected) 

(1 row(s) affected) 

하지 않도록 표시하지만 그게 문제라고 생각하지 않습니다. 여기

코드는 SP 호출 :

public static void Exec_Update_Query() 
     //updates a column of a single row, checks that the update succeeded, and then update it again to return it to the original value 
     { 
      string oldName = "Chai"; 
      string newName = "Chai Tea"; 

      SqlDatabase defaultDB = EnterpriseLibraryContainer.Current.GetInstance<Database>() as SqlDatabase; 

      // Create command to execute the stored procedure and add the parameters. 
      DbCommand cmd = defaultDB.GetStoredProcCommand("UpdateProductsTable"); 
      defaultDB.AddInParameter(cmd, "ProductID", DbType.Int32, 1); 
      defaultDB.AddInParameter(cmd, "ProductName", DbType.String, newName); 
      defaultDB.AddInParameter(cmd, "CategoryID", DbType.Int32, 1); 
      defaultDB.AddInParameter(cmd, "UnitPrice", DbType.Currency, 18); 

      // Execute the query and check if one row was updated. 
      int i = defaultDB.ExecuteNonQuery(cmd); 
      if (i == 1) 
      { 
       // Update succeeded. 
      } 
      else 
      { 
       Console.WriteLine("ERROR: Could not update just one row."); 
      } 

      // Change the value of the second parameter 
      defaultDB.SetParameterValue(cmd, "ProductName", oldName); 

      // Execute query and check if one row was updated 
      if (defaultDB.ExecuteNonQuery(cmd) == 1) 
      { 
       // Update succeeded. 
      } 
      else 
      { 
       Console.WriteLine("ERROR: Could not update just one row."); 
      } 
     } 

나는 방법의 반환 값을 볼 수 INT를 사용하고 있습니다 및이 될 이유 2. 어떤 아이디어를 반환? 이것은 SQL 2005에 대해 VS2010에서 실행되는 Enterprise Libarary 5.0입니다. 매우 직설적이지만 당혹 스럽습니다.

답변

5

올바르게 호출 한 경우 명령 결과로 실행되는 트리거의 결과도 반환 된 행 개수에 포함됩니다. 대부분이 문제입니다.

EDIT : 문서 MSDN SqlCommand.ExecuteNonQuery 가입일

는 :

트리거가 삽입되거나 업데이트되는 테이블에 존재하는, 리턴 값은 삽입 또는 갱신 동작과 양에 의해 영향 행수를 포함 트리거에 의해 영향을받는 행 수.

+0

Thx 오스틴, 나는 몰랐습니다! 이것이 바로 LastModified 필드를 설정하는 업데이트 트리거였습니다. 2의 값에 대해 유효성 검사 - LOL –

+0

트리거에 의해 영향을받는 행을 피하고 실제 dml 조작에 의해 영향을받는 행만 얻으려고합니까? –

+0

@ girishgupta211 - 나는 그렇게 생각하지 않는다. 그러나 그것은 지역 공동체에 물어보기에 좋은 질문 일 것이고, 우리는 여기에서 그것과 연결할 수있다. –

관련 문제