【发布时间】:2013-09-18 21:46:53
【问题描述】:
我正在尝试运行一个返回单个整数值的存储过程,但我不知道它哪里出错了。从我读过的内容来看,以下内容应该可以工作,但不能,而且我看不出哪里出错了。
这是我的存储过程:
ALTER PROCEDURE [dbo].[getDDNTempID]
AS
BEGIN
declare @tempID int
select top 1 @tempID = tempID from tblDDNHdr order by tempID asc
if @tempID is null
return 0
else
return @tempID
END
这是我尝试获取返回值的代码:
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["test"].ConnectionString))
{
SqlCommand cmd = new SqlCommand("getDDNTempID", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection.Open();
Int32 tempID = (Int32)cmd.ExecuteScalar();
if (tempID == 0) { tempID = -1; }
return tempID;
}
当这个过程被调用时,我得到一个 NullReferenceException 并且给出错误的行是:
Int32 tempID = (Int32)cmd.ExecuteScalar();
如果你们能提供任何指导,我将不胜感激。
谢谢
【问题讨论】:
-
这与这篇文章非常相似 [accessing-sql-server-stored-procedure-output-parameter-in-c-sharp][1] [1]: stackoverflow.com/questions/12174399/…
标签: c# sql-server stored-procedures