Returns values ​​from stored procedures in C #

I saw several similar questions to this, but none of them were the same. Anyway, I have a stored procedure that ends with lines,

select SCOPE_IDENTITY()
return

Then I get the value by calling executeScalar(). It works great. However, I feel that it would be better to just end with return select SCOPE_IDENTITY()(and not create a new parameter for this return value). Is it possible?

+5
source share
1 answer

, ... , , - "", , ADO.Net .

return ( )

OutPut ( proc , (proc) , ADO.net proc ...

dis-ad... , ... ( )

...

  Create Procedure MyProc
  @Name varchar(20),
  @DOB DateTime,
  @EmployeeId Integer Output = Null
  As
  Set NoCount On

     If @EmployeeId Is Null 
       Begin
          Insert Employees(Name, DateofBirth)
          Values (@Name, @DOB)
          Set @EmployeeId = Scope_Identity()
       End
     Else If Exists(Select * From Employees
                    Where EmployeeId =@EmployeeId)
       Begin
           Update Employees Set
              Name = Isnull(@Name, Name),
              DateOfBirth = IsNull(@DOB, DateOfBirth)
           Where EmployeeId = @EmployeeId
       End
     Else
        Raiserror('EmployeeId %d is missing or has been deleted.',
                   16, 1, @EmployeeId)

     Return 0

ADO.Net ... , , , ParameterDirection, ParameterDirection.InputOutput ParameterDirection.Output, ( )

+9

All Articles