2015-07-10 57 views
8

我有一個T-SQL存儲過程:小巧玲瓏調用存儲過程和地圖結果

CREATE PROCEDURE [dbo].[GetRequestTest] 
     @RequestId UNIQUEIDENTIFIER 
AS 
BEGIN 
    SELECT 
     Request.Amount, 
     Request.Checksum 
    FROM 
     Request 
    WHERE 
     RequestId = @RequestId 
END 

C#映射類:

public class CustomTest : Itest 
{ 
    public decimal Amount {get;set;} 
    public string Checksum { get; set; } 
} 

我打電話試圖通過調用存儲過程小巧玲瓏:

public void Load(CustomTest obj, Guid RequestId) 
{ 
    using (var con = base.GetClosedConnection()) 
    { 
     con.Open(); 

     var p = new DynamicParameters(); 
     p.Add("@RequestId", dbType: DbType.Guid, direction: ParameterDirection.Input);    

     var result = con.ExecuteReader("[dbo].[GetRequestTest]", param: p, commandType: CommandType.StoredProcedure); 

     while (result.Read()) 
      obj.Amount = (decimal)result["Amount"]; 
    }    
} 

但結果是空

我試圖調用將存儲過程中的SQL語句直接放到C#代碼中 - 它工作正常,但它不適用於存儲過程。

任何想法 - 如何使它工作?

回答

7

你叫錯了方法:

public void Load(CustomTest obj, Guid RequestId) 
{ 
    using (var con = base.GetClosedConnection()) 
    { 
     con.Open();     

     //result is list of CustomTest 
     var result = db.Query<CustomTest>("GetRequestTest", new {RequestId}, commandType: CommandType.StoredProcedure); 

    }    
} 

如何短小精悍使用:https://github.com/StackExchange/dapper-dot-net

1
using (var con = base.GetClosedConnection()) 
{ 
    var result = conn.Query<CustomTest>("exec [dbo].[GetRequestTest] @id", new {Id = RequestId}); 
}