我有以下 MS SQL 函数:
CREATE FUNCTION [dbo].[fn_NumApplications] ()
RETURNS int
AS
BEGIN
DECLARE @numRecords int = 0
SELECT @numRecords = COUNT(A.id) FROM Applications A
RETURN @numRecords
END
以及调用该函数的以下 C# 代码:
using (SqlConnection conn = new SqlConnection(mydbconnstring))
{
using (SqlCommand cmd = new SqlCommand("dbo.fn_numApplications", conn))
{
cmd.CommandType = CommandType.StoredProcedure;
//execute
conn.Open();
string retVal = cmd.ExecuteScalar().ToString();
conn.Close();
}
}
当函数到达 cmd.ExecuteScalar() 时,我得到一个 NullReferenceException。
我尝试使用和不使用“dbo.”来调用该函数。我也尝试过使用 cmd.ExecuteReader() 调用它,但没有成功。
这里出了什么问题?