我在访问
下面的存储过程中的输出时遇到问题 DbParameter DbParm = command.CreateParameter();
DbParm.Direction = ParameterDirection.Output;
DbParm.ParameterName = "@SomeValue";
DbParm.DbType = DbType.Int32;
DbParm.Value = null;
command.Parameters.Add(DbParm);
执行程序后
command.Parameters["@SomeValue"].Value;
好吧它总是返回Null,我可以访问第二个第二个选择。这是程序:
CREATE PROCEDURE SomeThing
@SomeValue int OUTPUT,
AS
SELECT @SomeValue=ID
FROM SomeTable
WHERE ID=10;
SELECT *
FROM SomeTable;
GO
答案 0 :(得分:4)
现在我假设你正在调用ExecuteNonQuery
,它不会要求结果集。 SQL Server将在第二次选择开始时暂停该过程。
如果您更改C#代码以使用ExecuteReader()
,则可以在阅读器完成后检索输出参数:
using (var read = yourCommand.ExecuteReader())
while (read.Read());
// You should be able to access the output parameter here
如果您向该过程添加更多流选择:
SELECT * FROM SomeTable where id = 1;
SELECT 'hello world';
SELECT * FROM SomeTable where null in (1,2,3);
您必须使用NextResult
using (var read = yourCommand.ExecuteReader()) {
do {
while (read.Read());
} while (read.NextResult());
}
将在分配输出参数之前。原因是输出参数的值可能取决于最后一次选择。因此SQL Server在完成整个存储过程之前不知道正确的值。
答案 1 :(得分:0)
我从未使用过dbparameter对象,只使用system.data.sqlclient.sqlparameter对象来完成将参数传递给SQL Server上的存储过程。请尝试使用SqlParameter。