简体   繁体   English

如何从DBContext中的存储过程向Web API控制器返回值

[英]How to return values to Web API Controller from a stored procedure in DBContext

I am trying validate values to be inserted into SQL from Web API 2 and Repository Pattern. 我正在尝试验证要从Web API 2和存储库模式插入SQL的值。 The database has a stored procedure for checking the values passed (foreign key dependencies etc). 该数据库具有一个存储过程,用于检查传递的值(外键依赖性等)。 The stored proc returns a -1 for each value that is invalid or else isValid=true. 对于每个无效或isValid = true的值,存储的proc返回-1。 I need to pass the param that is invalid to my repository/controller so i can return an http response message to the end user indicating what was invalid. 我需要将无效的参数传递给我的存储库/控制器,以便我可以向最终用户返回http响应消息,指示无效的内容。 Here's what I have so far in my DBContext. 到目前为止,这是我在DBContext中所拥有的。 How can I do this? 我怎样才能做到这一点?

public void Api_Event_Prefs_InsertUpdateCheck(int individualID, int eventID) {

        var p_PrefID = new SqlParameter() { ParameterName = "@PrefID", Value = System.Data.SqlTypes.SqlInt32.Null, Direction = ParameterDirection.InputOutput };
        var p_individualID = new SqlParameter() { ParameterName = "@IndividualID", Value = individualID, Direction = ParameterDirection.InputOutput };
        var p_eventID = new SqlParameter() { ParameterName = "@eventID", Value = eventID, Direction = ParameterDirection.InputOutput };
        var p_IsValid = new SqlParameter() { ParameterName = "@IsValid", Value = System.Data.SqlTypes.SqlBoolean.False, Direction = ParameterDirection.Output };

        this.Database.ExecuteSqlCommand("Exec dbo.Api_Event_Prefs_InsertUpdateCheck @PrefID, @IndividualID OUT, @EventID OUT, @IsValid OUT", p_PrefID, p_individualID, p_eventID, p_IsValid);

    }

You need to use Database.SqlQuery , which lets you execute an stored procedure, or any other query, and get the results of the execution. 您需要使用Database.SqlQuery ,它使您可以执行存储过程或任何其他查询,并获取执行结果。

ExecuteSqlCommand is only useful for running queries from which don't need to return the data (for example updates or DDL sentences). ExecuteSqlCommand仅适用于运行不需要从中返回数据(例如更新或DDL语句)的查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从MVC web api中的存储过程返回json - Return in json from stored procedure in MVC web api Web API调用存储过程并返回Result - Web API to call the stored Procedure and return Result 如何从web api控制器返回文件? - How to return a file from web api controller? 如何从从 SQL Server 存储过程获取 JSON 结果的 C# Web API 返回 JSON 结果? - How to return JSON result from C# Web API that gets JSON result from SQL Server Stored Procedure? Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller - Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller 用于存储过程的ASP.NET Web Api控制器不返回数据 - ASP.NET Web Api controller for stored procedure not returning data 从LINQ调用存储过程(使用dbcontext) - Call stored procedure from LINQ (with dbcontext) 从SQL Server存储过程向Web API C#返回自定义消息 - Return a custom message from SQL Server stored procedure to Web API C# Web API不会使用存储过程返回JSON - Web API won't return JSON using stored procedure 如何在一个控制器/JOIN 中从我的数据库返回/获取两个模型/表或使用存储过程 - How to return / GET two models/tables from my DB in one controller / JOIN or use stored procedure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM