2012-09-09 7 views
7

ecco la mia stored procedure. quando lo collaudo, ottengo sempre risultati corretti.Utilizzando Dapper.net per chiamare la stored procedure, restituire sempre -1 back

ALTER PROCEDURE [dbo].[AddSmoothieIngredients] 
    -- Add the parameters for the stored procedure here 
    @Query NVARCHAR(4000) , 
    @SmoothieId INT , 
    @CreatedDate DATETIME , 
    @Status INT , 
    @UserId INT 
AS 
    BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
     SET NOCOUNT ON; 

     BEGIN TRY 
      BEGIN TRAN 

      IF @SmoothieId > 0 
       BEGIN 
        DELETE FROM dbo.SmoothieIngredients 
        WHERE SmoothieId = @SmoothieId; 

        EXECUTE (@Query); 
       END 
      ELSE 
       BEGIN 

        IF @UserId = 0 
         SET @UserId = NULL; 

        INSERT INTO dbo.Smoothie 
          (Name, CreatedDate, Status, UserId) 
        VALUES (N'', @CreatedDate, @Status, @UserId); 

        SET @SmoothieId = SCOPE_IDENTITY(); 

        SET @Query = REPLACE(@Query, 'sId', @SmoothieId); 
        EXECUTE (@Query); 

       END 


      COMMIT TRAN 

      RETURN @SmoothieId 

     END TRY 

     BEGIN CATCH 
      ROLLBACK 
     END CATCH 

    END 

Tuttavia Quando chiamo questa stored procedure utilizzando dapper.net, restituisco sempre -1 indietro.

 using (var conn = OpenConnection()) 
     { 
      var parameter = new { Query = query, SmoothieId = smoothieId, CreatedDate = createdDate, Status = status, UserId = userId }; 
      return conn.Execute("AddSmoothieIngredients", parameter, commandType: CommandType.StoredProcedure); 
     } 

Probabilmente dapper.net non è in grado di rilevare il valore restituito dalla stored procedure. ma davvero non so come aggiustarlo per favore aiuto.

risposta

2

Sembra che Dapper.net utilizzi SqlCommand.ExecuteNonQuery per il metodo Execute. Ciò restituisce il numero di righe interessate, non il valore dell'istruzione return. Quello che stai cercando è Query

return connection.Query<int>("AddSmoothieIngredients", parameter, commandType: CommandType.StoredProcedure).First(); 

Anche se non credo che catturerà l'istruzione return o, nel qual caso è necessario modificare la stored procedure per restituire un set di risultati.

ALTER PROCEDURE [dbo].[AddSmoothieIngredients] 
    -- Add the parameters for the stored procedure here 
    @Query NVARCHAR(4000) , 
    @SmoothieId INT , 
    @CreatedDate DATETIME , 
    @Status INT , 
    @UserId INT 
AS 
BEGIN 
-- SET NOCOUNT ON added to prevent extra result sets from 
-- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    BEGIN TRY 
     BEGIN TRAN 

     IF @SmoothieId > 0 
      BEGIN 
       DELETE FROM dbo.SmoothieIngredients 
       WHERE SmoothieId = @SmoothieId; 

       EXECUTE (@Query); 
      END 
     ELSE 
      BEGIN 

       IF @UserId = 0 
        SET @UserId = NULL; 

       INSERT INTO dbo.Smoothie 
         (Name, CreatedDate, Status, UserId) 
       VALUES (N'', @CreatedDate, @Status, @UserId); 

       SET @SmoothieId = SCOPE_IDENTITY(); 

       SET @Query = REPLACE(@Query, 'sId', @SmoothieId); 
       EXECUTE (@Query); 

      END 


     COMMIT TRAN 

     SELECT @SmoothieId 
     RETURN 
    END TRY 

    BEGIN CATCH 
     ROLLBACK 
    END CATCH 

END 

Oppure è possibile utilizzare un altro metodo di accesso DB.

+0

grazie. ma devi ancora usare execute, ho trovato un altro esempio online e funziona. – qinking126

13

Trovato la soluzione, ecco il codice di esempio che ho trovato online. e funziona

var p = new DynamicParameters(); 
p.Add("@a", 11); 
p.Add("@b", dbType: DbType.Int32, direction: ParameterDirection.Output); 
p.Add("@c", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); 

cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 

int b = p.Get<int>("@b"); 
int c = p.Get<int>("@c"); 
14

Quindi la ragione per cui Execute() restituisce -1 è perché il tuo sProc ha SET NOCOUNT ON; che "sopprime il '' messaggio dopo ogni DML" filari xx colpite in base alle this question. Se si desidera disabilitarlo o meno è un'altra questione discussa anche in quel collegamento.

Ho appena incontrato lo stesso problema, quindi ho pensato di buttare i miei 2 centesimi.

+0

Non lo sapevo mai! +1 – nawfal

1

Recentemente ho dovuto modificare una procedura esistente per restituire un valore aggiuntivo, e avevo utilizzato un tipo anonimo per passare i parametri. In questo contesto, la cosa veramente bella è che DynamicParameters supporta un tipo anonimo come parametro di input, il che rende questa modifica facile da implementare.

ho il seguente:

cnn.Execute("spMagicProc", new { a = 11, x = 13, y = 14, z = "something" }, 
    commandType: commandType.StoredProcedure); 

ero cambiamento può che:

var p = new DynamicParameters(new { a = 11, x = 13, y = 14, z = "something" }); 
p.Add("@rval", dbType: DbType.Int32, direction: ParameterDirection.ReturnValue); 

cnn.Execute("spMagicProc", p, commandType: commandType.StoredProcedure); 

int rval = p.Get<int>("@rval");