본문 바로가기

C#

Passing Output parameters to stored procedure using dapper

728x90

쿼리

CREATE PROCEDURE SP_MYTESTpROC
    @VAR1 VARCHAR(10),
    @VAR2 VARCHAR(20),
    @BASEID INT ,
    @NEWID INT OUTPUT
As Begin
   INSERT INTO TABLE_NAME(username, firstname)
      select @VAR1, @VAR2 
      WHERE ID = @BASEID

   SET @NEWID = SCOPE_IDENTITY() AS INT
END

 

C# 코드

public void InsertData()
{
	var p = new DynamicParameters();
	p.Add("VAR1", "John");
	p.Add("VAR2", "McEnroe");
	p.Add("BASEID", 1);
	p.Add("NEWID", dbType: DbType.Int32, direction: ParameterDirection.Output);
	connection.Query<int>("SP_MYTESTpROC", p, commandType: CommandType.StoredProcedure);
	int newID =  p.Get<int>("NEWID");
}

[출처]

https://stackoverflow.com/questions/22353881/passing-output-parameters-to-stored-procedure-using-dapper-in-c-sharp-code

728x90