Matheus Lima
Matheus Lima

Reputation: 2143

Return ID with INSERT INTO (conn.Execute)

I'm inserting a new value using:

var message = conn.Execute("INSERT INTO [Message] (SenderId, Text, Date) values (@passeiDiretoUserId, @text, @date) SELECT SCOPE_IDENTITY()",
                new { @id, @text = send.Text, @date });

The query is working fine, but I need the ID of the value that was just created. Thanks!

Upvotes: 0

Views: 393

Answers (1)

Rahul Tripathi
Rahul Tripathi

Reputation: 172518

You are probably looking for ExecuteScalar

Executes the query, and returns the first column of the first row in the result set returned by the query. Additional columns or rows are ignored.

Upvotes: 2

Related Questions