小巧玲珑调用存储过程和地图结果
问题描述:
类
我有一个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#代码中 - 它工作正常,但它不适用于存储过程。
任何想法 - 如何使它工作?
答
你叫错了方法:
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);
}
}
答
using (var con = base.GetClosedConnection())
{
var result = conn.Query<CustomTest>("exec [dbo].[GetRequestTest] @id", new {Id = RequestId});
}