【问题标题】:Run a SQL Server stored procedure directly from controller直接从控制器运行 SQL Server 存储过程
【发布时间】:2020-08-24 12:04:38
【问题描述】:

我在 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());
    }
}

但是,我收到以下错误:

System.InvalidOperationException: '结果类型'System.Data.Entity.Core.Objects.ObjectResult' 可能不是抽象的,必须包含默认构造函数。'

我也试过下面的代码,结果是"-1"

var query = entities.Database.ExecuteSqlCommand("exec [dbo].[current_call] @call_id", new SqlParameter("call_id", c_id)).toString();

【问题讨论】:

标签: c# asp.net-mvc entity-framework stored-procedures entity-framework-core


【解决方案1】:

SqlQuery 不适用于动态类型:

如果你想使用动态类型或匿名类型作为它的 [SqlQuery] 返回类型,您可能会编译您的代码但收到 运行时异常。

另外请注意,您不能只调用ToString() 将您的List&lt;object&gt; 转换为json,您需要serialize 他们。

注意:在这个答案中,我将使用Newtonsoft.Json 进行序列化,但是还有其他options


我建议为您的 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 }
}

那么就可以得到结果了:

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

如果你想将结果返回为序列化的json,那么试试:

var jsonResult = JsonConvert.SerializeObject(currentCalls); 
return Json(jsonResult)

【讨论】:

  • 谢谢!它正在对stored-procedure 的调用进行小修改。我编辑了你的回复。 entities.Database.SqlQuery&lt;List&lt;CurrentCall&gt;&gt; 不需要。相反,我使用的是entities.Database.SqlQuery&lt;CurrentCall&gt;
猜你喜欢
  • 2020-02-24
  • 2016-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-07
  • 2018-09-01
相关资源
最近更新 更多