简体   繁体   中英

How can I run a stored procedure that has no return values from C# with my db context?

I am using the following code to run a stored procedure:

MyContext db = new MyContext();
var a = db.Database.SqlQuery<string>("dbo.getQuestionUIdsByTopicId @TopicId",
           new SqlParameter { ParameterName = "TopicId", Value = testTopicId });

This works good but now I will have a stored procedure that does not return any data other than a return code.

How can I execute a stored procedure with a parameter using my context db.Database and have the stored procedure return only a return code? If someone could give an example of a 3-4 line SP and how it returns a return code that would also be a great help.

You can use ExecuteSqlCommand to send non-query commands to the database.

int result = db.Database.ExecuteSqlCommand("exec sproc...");

See this link for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM