Dapper调用存储过程并将结果映射到类

时间:2021-09-06 02:05:13

I have a T-SQL stored procedure:

我有一个T-SQL存储过程:

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

C# mapping class:

C#映射类:

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

I'm calling trying to invoke stored procedure by using Dapper:

我正在调用尝试使用Dapper调用存储过程:

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"];
    }            
}

But result is null

但结果为空

I tried to call to put SQL statement from stored procedure directly into C# code - and it works fine, but it doesn't work with stored procedure.

我试图调用将存储过程中的SQL语句直接放入C#代码 - 它工作正常,但它不适用于存储过程。

Any ideas - how to make it work?

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

2 个解决方案

#1


9  

You call wrong method:

你打错了方法:

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);
    }            
}

How to use dapper: https://github.com/StackExchange/dapper-dot-net

如何使用dapper:https://github.com/StackExchange/dapper-dot-net

#2


1  

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

Column names stored procedure or query returns should be same as CustomTest`s property names (e.g. Amount, Checksum). As result you will receive IEnumerable filled with appropriate data.

列名称存储过程或查询返回应与CustomTest的属性名称相同(例如Amount,Checksum)。因此,您将收到填充了适当数据的IEnumerable。

#1


9  

You call wrong method:

你打错了方法:

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);
    }            
}

How to use dapper: https://github.com/StackExchange/dapper-dot-net

如何使用dapper:https://github.com/StackExchange/dapper-dot-net

#2


1  

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

Column names stored procedure or query returns should be same as CustomTest`s property names (e.g. Amount, Checksum). As result you will receive IEnumerable filled with appropriate data.

列名称存储过程或查询返回应与CustomTest的属性名称相同(例如Amount,Checksum)。因此,您将收到填充了适当数据的IEnumerable。