C# 直接从控制器运行SQL Server存储过程

C# 直接从控制器运行SQL Server存储过程,c#,asp.net-mvc,entity-framework,stored-procedures,entity-framework-core,C#,Asp.net Mvc,Entity Framework,Stored Procedures,Entity Framework Core,我在SQL Server中有以下存储过程: CREATE PROCEDURE current_call @call_id int AS BEGIN SET NOCOUNT ON; SELECT dbo.Call.call_id, dbo.Call.city, dbo.Call.call_received, dbo.Call.call_transfer, dbo.Call.call_response, dbo.Call.call_dep

我在SQL Server中有以下存储过程:

CREATE PROCEDURE current_call
    @call_id int
AS
BEGIN
    SET NOCOUNT ON;

    SELECT 
        dbo.Call.call_id, dbo.Call.city, dbo.Call.call_received, 
        dbo.Call.call_transfer, dbo.Call.call_response, dbo.Call.call_depart, 
        dbo.Call.call_arrive, dbo.Call.call_return, dbo.Call.call_end, 
        dbo.Priority.name AS priorityName, dbo.Call_Status.name as Call_Status, 
        dbo.Station.station_name, dbo.City.city_name, dbo.Protocol.name AS protocolName
    FROM     
        dbo.City 
    INNER JOIN
        dbo.Call 
    INNER JOIN 
        dbo.Priority ON dbo.Call.priority = dbo.Priority.priority_id 
        ON dbo.City.city_id = dbo.Call.city 
    INNER JOIN
        dbo.Protocol ON dbo.Call.protocol_category = dbo.Protocol.id 
    LEFT OUTER JOIN
        dbo.Station 
    INNER JOIN
        dbo.Ambulance ON dbo.Station.station_id = dbo.Ambulance.ambulance_station 
        ON dbo.Call.amb_id = dbo.Ambulance.ambulance_id
    INNER JOIN
        dbo.Call_Status ON dbo.Call.call_status = dbo.Call_Status.call_status_id
    WHERE 
        dbo.Call.call_id = @call_id;
END
我试图通过一个控制器调用它,并将检索到的字段打印为json字符串,但运气不好

这是我的控制器:

public System.Web.Http.Results.JsonResult<String> GetTwo(int c_id)
{
    using (EMSMVCEntities entities = new EMSMVCEntities())
    {
        entities.Configuration.ProxyCreationEnabled = false;

        var query = entities.Database.SqlQuery<ObjectResult>("exec [dbo].[current_call] @call_id", new SqlParameter("call_id", c_id)).ToList();

        return Json (query.ToString());
    }
}
不适用于动态类型:

如果要使用动态类型或匿名类型作为其[SqlQuery] 返回类型,您可能会编译代码,但收到 运行时出现异常

还要注意的是,您不能只调用ToString将列表转换为json,您需要调用它们

注意:在这个答案中,我将用于序列化,但是还有其他的

我建议为您的SP结果创建一个模型,这样您就可以将SqlQuery的结果投影到您的模型中。。。定义模型还有使用[JsonProperty]属性的好处,这有助于在将属性序列化为json时将属性映射到不同的名称

public class CurrentCall
{
    // [JsonProperty(PropertyName = "Caller_Id")] <-- json property name (if different)
    public long call_id { get; set }
    public string city { get; set }
    public DateTime call_received { get; set }
    public string call_transfer { get; set }
    public string call_response { get; set }
    public DateTime call_depart { get; set }
    public DateTime call_arrive { get; set }
    public DateTime call_return { get; set }
    public DateTime call_end { get; set }
    public string priorityName { get; set }
    public bool Call_Status { get; set }
    public string station_name { get; set }
    public string city_name { get; set }
    public string protocolName { get; set }
}
不适用于动态类型:

如果要使用动态类型或匿名类型作为其[SqlQuery] 返回类型,您可能会编译代码,但收到 运行时出现异常

还要注意的是,您不能只调用ToString将列表转换为json,您需要调用它们

注意:在这个答案中,我将用于序列化,但是还有其他的

我建议为您的SP结果创建一个模型,这样您就可以将SqlQuery的结果投影到您的模型中。。。定义模型还有使用[JsonProperty]属性的好处,这有助于在将属性序列化为json时将属性映射到不同的名称

public class CurrentCall
{
    // [JsonProperty(PropertyName = "Caller_Id")] <-- json property name (if different)
    public long call_id { get; set }
    public string city { get; set }
    public DateTime call_received { get; set }
    public string call_transfer { get; set }
    public string call_response { get; set }
    public DateTime call_depart { get; set }
    public DateTime call_arrive { get; set }
    public DateTime call_return { get; set }
    public DateTime call_end { get; set }
    public string priorityName { get; set }
    public bool Call_Status { get; set }
    public string station_name { get; set }
    public string city_name { get; set }
    public string protocolName { get; set }
}

好吧,好吧,谢谢你!它正在对存储过程的调用进行小的编辑。我编辑了你的回复。不需要entities.Database.SqlQuery。相反,我使用的是entities.Database.SqlQuery.Thank!它正在对存储过程的调用进行小的编辑。我编辑了你的回复。不需要entities.Database.SqlQuery。相反,我使用的是entities.Database.SqlQuery。
var currentCalls = entities.Database.SqlQuery<CurrentCall>("exec [dbo].[current_call] @call_id", new SqlParameter("call_id", c_id)).ToList();
var jsonResult = JsonConvert.SerializeObject(currentCalls); 
return Json(jsonResult)