当查看存储过程时,逻辑似乎是“执行操作”,然后是 select 1 as add_success
。我如何检查 C# 中的 add_success
值?
代码
using (SqlConnection connection = new SqlConnection(_connectionString)) {
using (SqlCommand command = new SqlCommand("<sprocname>", connection)) {
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(<a parameter>); // x10 or so
connection.Open();
var result = command.ExecuteNonQuery();
// result is always -1, despite the sproc doing everything it is supposed to do (modify a few different tables)
存储过程
// Do some stuff including inserts on a few different tables, then
INSERT INTO Table (field1, field2)
VALUES (@Val1, @Val2)
SELECT 1 AS add_success
似乎一切都正确,但 ExecuteNonQuery
的默认返回是受影响的行,并且始终是 -1
,即使所有操作似乎都已完成成功地。
我还尝试添加一个返回值名为 add_success
和 @add_success
的参数,但这似乎也不起作用。
如何从存储过程向 C# 返回值?
最佳答案
- 您需要使用
command.ExecuteScalar()
。 - 结果将是结果集中第一个表的第一行。
关于c# - 如何从 C# 中的存储过程中获取选定的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15577306/