Excute store procedure with output parameter using DBContext Entity Framework

This code below is ExecuteSqlCommand with output parameter


           // Store Procedure for examplle  ALTER proc[dbo].[WithdrawRequest]
            //        (
            //   @UserId varchar(128)
            // ,@Amount money
            //  ,@Status int
            // ,@Description nvarchar(50)
            // ,@PaymentType int
            // ,@WalletId varchar(50)
            // ,@ErrorMsg nvarchar(50) output
            //)

            var UserId = new SqlParameter("@UserId", userId);
            var Amount = new SqlParameter("@Amount", model.Amount);            
            var Description = new SqlParameter("@Description", "");
            var PaymentType = new SqlParameter("@PaymentType", model.PaymentType);
            var WalletId = new SqlParameter("@WalletId", model.WalletId);           
            var Status = new SqlParameter
            {
                ParameterName = "@Status",
                DbType = System.Data.DbType.Int32,
                Value = 0
            };
           //Output
          var ErrorMsg = new SqlParameter
            {
                ParameterName = "@ErrorMsg",
                DbType = System.Data.DbType.String,
                Size = 500,
                Direction = System.Data.ParameterDirection.Output
            };
            string excuteSP = "exec dbo.WithdrawRequest @UserId, @Amount, @Status, @Description, @PaymentType, @WalletId, @ErrorMsg OUT";
            //ExecuteSqlCommand with output parameter
            var resp = db.Database.ExecuteSqlCommand(excuteSP, UserId, Amount, Status, Description, PaymentType, WalletId, ErrorMsg);            
            string result = ErrorMsg.Value.ToString();
Reactions

Post a Comment

0 Comments