I'm new to Dapper - please help me. How can I get the inserted record value after a successful insert?
Stored procedure:
ALTER PROCEDURE Sp_InsertTestData
@Name varchar(50),
@gender int,
@refres int OUTPUT
AS
BEGIN
INSERT INTO Test_Master (Name, Gender)
VALUES (@Name, @gender);
SELECT @refres = SCOPE_IDENTITY()
SELECT @refres as M_SID
END
When I execute this stored procedure in SQL like this:
DECLARE @refres INT
EXEC Sp_InsertTestData 'test12',1,@refres
I'm getting an output showing the last inserted row's value.
But when this stored procedure is executed from C# code, every time I'm getting a value of 1:
using (SqlConnection con = new SqlConnection(_configuration.GetConnectionString("DatabaseConnection")))
{
con.Open();
SqlTransaction sqltrans = con.BeginTransaction();
var param = new DynamicParameters();
param.Add("@Name", Bindtestmaster.Name);
param.Add("@gender", Bindtestmaster.Gender);
param.Add("@refres");
res = con.Execute("Sp_InsertTestData", param, sqltrans, 0, CommandType.StoredProcedure);
}
resis a count of the number of records updated which will be one. What does the database hold after insertsp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!