理想情況下,我試圖讓存儲過程返回1(如果存在),否則返回0。SQL Server存儲過程(如果存在)
這是存儲過程:
CREATE PROCEDURE [dbo].[spCheckForExistingTimecard]
@userId int,
@paYPeriodId int,
@exists bit = 0 OUTPUT
AS
BEGIN
IF EXISTS (SELECT COUNT (t.TimeCardId)
FROM TimeCard AS t
WHERE t.PayPeriodId = @payPeriodId
AND t.UserId = @userId)
RETURN 1
ELSE
RETURN 0
這裏是調用存儲過程的代碼:
public static int CheckForExistingTimecard(int userId, int payPeriodId)
{
using (SqlConnection connection = new SqlConnection(dbMaintenanceConnectionString))
{
connection.Open();
using (SqlCommand sqlCommand = new SqlCommand("spCheckForExistingTimecard", connection))
{
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Parameters.AddWithValue("@userId", userId);
sqlCommand.Parameters.AddWithValue("@payPeriodId", payPeriodId);
return (int)sqlCommand.ExecuteScalar();
}
}
}
問題是,我得到一個錯誤不設置
對象引用到一個對象的實例
上的調用代碼的返回行。
任何幫助,將不勝感激