Created
March 18, 2020 19:30
-
-
Save gavilanch/00380ffc4ac5981ea8e0ac53a57a84db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Stored procedure returns ID | |
ALTER PROCEDURE [dbo].[InsertValue] | |
-- Add the parameters for the stored procedure here | |
@Value1 int, | |
@Value2 nvarchar(500), | |
@Id int OUTPUT | |
AS | |
BEGIN | |
-- SET NOCOUNT ON added to prevent extra result sets from | |
-- interfering with SELECT statements. | |
SET NOCOUNT ON; | |
insert into [Values] (Value1, Value2) | |
values (@Value1, @Value2) | |
Set @Id = SCOPE_IDENTITY() | |
END | |
// Changes to the insert method | |
public async Task Insert(Value value) | |
{ | |
using (SqlConnection sql = new SqlConnection(_connectionString)) | |
{ | |
using (SqlCommand cmd = new SqlCommand("InsertValue", sql)) | |
{ | |
cmd.CommandType = System.Data.CommandType.StoredProcedure; | |
cmd.Parameters.Add(new SqlParameter("@value1", value.Value1)); | |
cmd.Parameters.Add(new SqlParameter("@value2", value.Value2)); | |
var valueIdParameter = cmd.Parameters.Add(new SqlParameter("@Id", value.Value2)); | |
valueIdParameter.Direction = ParameterDirection.Output; | |
await sql.OpenAsync(); | |
await cmd.ExecuteNonQueryAsync(); | |
var id = int.Parse(valueIdParameter.Value.ToString()); | |
return; | |
} | |
} | |
} |
Hello Felipe, How do you get this returned 'id' in the calling controller action?
Hope to hear from you soon.
Cheers....
how can we create login authentication and log out in this project by protecting some route, and also creating admin route.Thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one Felipe. Nicely implemented. What about the call in the controller method to show how this returned Id is handled or passed from repo to the controller's Create Action... Let's say I need to use this Id to implement another action while implementing The Create action in the controller?
Cheers....