Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SQL Server存储过程(如果存在)_C#_Sql_Sql Server_Stored Procedures - Fatal编程技术网

C# SQL Server存储过程(如果存在)

C# SQL Server存储过程(如果存在),c#,sql,sql-server,stored-procedures,C#,Sql,Sql Server,Stored Procedures,理想情况下,我尝试让存储过程返回1(如果存在),或者0(如果不存在) 这是存储过程: CREATE PROCEDURE [dbo].[spCheckForExistingTimecard] @userId int, @paYPeriodId int, @exists bit = 0 OUTPUT AS BEGIN IF EXISTS (SELECT COUNT (t.TimeCardId) FROM TimeCard AS t

理想情况下,我尝试让存储过程返回1(如果存在),或者0(如果不存在)

这是存储过程:

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();
            }
        }
    }
下面是调用存储过程的代码:

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();
            }
        }
    }
问题是我得到了一个错误

对象引用未设置为对象的实例

在调用代码的返回行上


如有任何帮助,将不胜感激。

请登录officeil网站

结果集中第一行的第一列,或null 如果结果集为空,则引用(Visual Basic中无任何内容)。 返回最多2033个字符

如果查询未返回任何记录,ExecuteScalar将返回null

所以这一行:

return(int)sqlCommand.ExecuteScalar()

抛出错误

因为在这种情况下,它试图将null转换为int。这将引发NullReferenceException

您需要检查空值:

object o = sqlCommand.ExecuteScalar();
item = o == null ? 0 : (int)o;

RETURN
中的值可以通过
SqlParameter
.Direction=ParameterDirection.ReturnValue
处理。
.ExecuteScalar()
将捕获的值是存储过程中的
SELECT
返回的单行

public static int CheckForExistingTimecard(int userId, int payPeriodId)
{
   using (SqlConnection connection = new SqlConnection(dbMaintenanceConnectionString))
   using (SqlCommand sqlCommand = new SqlCommand("spCheckForExistingTimecard", connection))
   {
       sqlCommand.CommandType = CommandType.StoredProcedure;
       sqlCommand.Parameters.AddWithValue("@userId", userId);
       sqlCommand.Parameters.AddWithValue("@payPeriodId", payPeriodId);

       -- define your parameter for the RETURN value
       sqlCommand.Parameters.Add("@ReturnValue").Direction = ParameterDirection.ReturnValue;

       connection.Open();
       sqlCommand.ExecuteNonQuery();

       -- read the value returned
       int returnValue = (int)sqlCommand.Parameters["@ReturnValue"];

       connection.Close();

       return returnValue;
   }
}