因此,我试图使用pl/pgSQL从一个web应用程序执行Dapper micro-ORM函数。无论在QueryMultiple方法中作为参数传递的值如何,我总是获得空值;尽管在数据库中,该函数给出了适当的结果。
DAL方法:
public List<JSONData> ExecutePostgres(string spName, List<QueryParam> Params)
{
List<JSONData> students = new List<JSONData>();
using (var dbInstance = new NpgsqlConnection(postgreConnectionString))
{
using (var reader = dbInstance.QueryMultiple(spName, new { std_id = Params[0].value }, commandType: CommandType.StoredProcedure))
{
students = reader.Read<JSONData>().ToList();
}
}
return students;
}实现方法:
public List<string> ExecuteProcedure(string spName, List<QueryParam> Params)
{
var list = objDLOperation.ExecutePostgres(spName, Params);
var strList = new List<string>();
list.ForEach(x => strList.Add(x.JSONResult));
return strList;
}此strList始终是null值的列表。
实体类:
public class JSONData
{
public string JSONResult { get; set; }
}pl/pgSQL函数:
CREATE OR REPLACE FUNCTION public.usp_getstudent(std_id integer)
RETURNS SETOF json
AS $function$
BEGIN
RETURN QUERY
select row_to_json(result) from (select * from student where id = std_id) as result;
END
$function$ LANGUAGE 'plpgsql';发布于 2017-05-10 12:21:42
您的列似乎名为result,或者是匿名的--但是您的本地类型有一个名为JSONResult的属性。达普对名字很挑剔。如果你让名字匹配的话,应该会很好的。然而,坦率地说:
<string> (这也绕过了命名问题-- dapper不关心单个列获取的名称)QueryMultiple --只需使用Query<T>作为一个非常的小点:.AsList()将比.ToList()更高效-避免任何额外的复制。
就我个人而言,我会说“返回类型的列,而不是JSON",但是:这取决于您。
https://stackoverflow.com/questions/43890589
复制相似问题