I am trying to insert a record into my database and retrieve the GUID it just added in.
Let's say if I have a table with 3 columns, GUID
, FirstName
, LastName
. I need to insert a new record and then get back the GUID that was just generated. The problem is that first and last name are duplicated, often. I am not quite sure how to accomplish
Here is what I tried, I know the below won't work as I am not really telling it which column to select back and I'm not sure how to tell it:
var query = @"INSERT INTO MyTable(GUID, FirstName, LastName)
SELECT
@GUID, @FirstName, @LastName);
using (var oConn = CreateConnection())
{
var test = oConn.Query<string>(query, new
{
GUID = Guid.NewGuid(),
"John",
"Doe"
}).Single();
}
The error that I get is
Sequence contains no elements