Getting RETURN @@ IDENTITY value in C #

Here is a very simple question. I have an SP that inserts a row into the table, and at the end the RETURN @@ IDENTITY statement appears. What I cannot find is a way to get this value in C #. I use the Enterprise library and use the method:

db.ExecuteNonQuery(cmd);

I tried cmd.Parameters [0] .Value to get a value, but which returns 0 all the time. Any ideas?

+3
source share
5 answers
Dim c as new sqlcommand("...")

Dim d As New SqlParameter()
d.Direction = ParameterDirection.ReturnValue
c.parameters.add(d)

c.executeNonQuery

(@@IDENTITY) = d.value

It more or less looks like this ... either this, or simply return the value from the stored procedure as an output parameter.

+4
source

, SCOPE_IDENTITY(), @@IDENTITY. .

+5

. SCOPE_IDENTITY, INSERT. , @@IDENTITY . , TRIGGERS , INSERT.

. Books Online.

+2

,

SqlCommand.ExecuteScalar()

, , SCOPE_IDENTITY() , @@IDENTITY

0

Source: https://habr.com/ru/post/1698858/


All Articles