简体   繁体   中英

How can I handle multiple fields returned from my stored procedure to a C# calling program?

I want to be able to return the following data from variables I have populated in my SP:

@RC - a one or zero return code
@AnswerGridCorrect - This would be a VARCHAR(20)

I am calling my stored procedure like this but I am not sure how I can get the data that comes back. Do I have to do a SELECT at the end of the SP to make these be returned. Also how can I read the data in my C# code.

Here is what I am doing now:

await db.Database.ExecuteSqlCommandAsync(sql, parameters);

I was just working this out this morning. The EF documentation covers this exact topic, based on SPs, but I see nothing in the process that assumes the SQL being executed is an SP rather than an arbitrary set of SQL statements returning multiple result sets (since it is using the underlying ADO.NET data reader).

The page is here: Stored Procedures with Multiple Result Sets .

In summary, for code first:

Use your context's Database.Connection property to open the connection. create a DbCommand and execute it (for a DbDataReader ). Then use your context's implementation of IObjectContextAdapter to translate each result set into EF entities (and start tracking).

For model first (or model from database) you need to edit the model's XML definition, and then use the type returned by the SPs wrapper in the generate code.

I believe you'd want to use output parameters, see if you can adapt this example to meet your needs:

http://support.microsoft.com/kb/310070

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