[英]Running a stored procedure in ASP.Net Core 2.2
Why is it that this line of code is not recognized in my asp.net web api 为什么我的asp.net Web API无法识别这一行代码
db.Database.Initialize(force: false);
var connection = db.Database.Connection
It works in my Desktop application but not in my webapi. 它可以在我的桌面应用程序中工作,但不能在我的webapi中工作。 I have installed the nuget package "EntityFramework" 6.2.0 The reason I need to get this is because I need to run this command: 我已经安装了nuget包“ EntityFramework” 6.2.0。我需要得到这个原因是因为我需要运行以下命令:
decimal? paymentAmount = model.PaymentAmount;
int? paymentTypeId = model.PaymentTypeId;
string OrNumber = model.ORNumber;
string ChequeNo = model.ChequeNo;
decimal? CWTax = model.CWTax;
int? unitId = model.UnitId;
DateTime currentDateTime = DateTime.Now;
var db = new ApplicationDbContext();
db.Database.Initialize(force: false);
using(var connection = db.Database.Connection)
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = "[dbo].[DUES_AUTO_PAYMENT]";
cmd.CommandType = CommandType.StoredProcedure;
DbParameter pPaymentAmount = cmd.CreateParameter();
pPaymentAmount.ParameterName = "@PaymentAmount";
pPaymentAmount.DbType = DbType.Decimal;
pPaymentAmount.Direction = ParameterDirection.Input;
pPaymentAmount.Value = model.PaymentAmount;
DbParameter pCwTax = cmd.CreateParameter();
pCwTax.ParameterName = "@CwTax";
pCwTax.DbType = DbType.Decimal;
pCwTax.Direction = ParameterDirection.Input;
pPaymentAmount.Value = model.PaymentAmount;
DbParameter pPaymentTypeId = cmd.CreateParameter();
pPaymentTypeId.ParameterName = "@PaymentTypeID";
pPaymentTypeId.DbType = DbType.Int32;
pPaymentTypeId.Direction = ParameterDirection.Input;
pPaymentTypeId.Value = model.PaymentTypeId;
DbParameter pOrNumber = cmd.CreateParameter();
pOrNumber.ParameterName = "@ORNumber";
pOrNumber.DbType = DbType.String;
pOrNumber.Direction = ParameterDirection.Input;
pOrNumber.Value = model.ORNumber;
DbParameter pChequeNo = cmd.CreateParameter();
pChequeNo.ParameterName = "@ChequeNo";
pChequeNo.DbType = DbType.String;
pChequeNo.Direction = ParameterDirection.Input;
pChequeNo.Value = model.ChequeNo;
DbParameter pUnitId = cmd.CreateParameter();
pUnitId.ParameterName = "@UnitID";
pUnitId.DbType = DbType.Int32;
pUnitId.Direction = ParameterDirection.Input;
pUnitId.Value = model.UnitId;
DbParameter pCurrentDateTime = cmd.CreateParameter();
pCurrentDateTime.ParameterName = "@CurrentDateTime";
pCurrentDateTime.DbType = DbType.DateTime2;
pCurrentDateTime.Direction = ParameterDirection.Input;
pCurrentDateTime.Value = DateTime.Now;
cmd.Parameters.Add(pPaymentAmount);
cmd.Parameters.Add(pPaymentTypeId);
cmd.Parameters.Add(pOrNumber);
cmd.Parameters.Add(pUnitId);
cmd.Parameters.Add(pCurrentDateTime);
try
{
var x = cmd.ExecuteNonQuery();
if(x > 0)
{
return Request.CreateResponse(res.IsSuccessStatusCode);
} else
{
return Request.CreateErrorResponse(res.StatusCode = HttpStatusCode.BadRequest, "Server could not execute the procedure.");
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(res.StatusCode = HttpStatusCode.BadRequest, ex.Message);
}
}
Can you please help me with this. 你能帮我这个忙吗? Thank you very much. 非常感谢你。
For running procedure, you could get the connection by Database.GetDbConnection()
. 对于运行过程,可以通过Database.GetDbConnection()
获得连接。
Try something like below: 尝试如下所示:
public class HomeController : Controller
{
private readonly ApplicationDbContext _context;
public HomeController(ApplicationDbContext context)
{
_context = context;
}
public async Task<IActionResult> Index()
{
using (var connection = _context.Database.GetDbConnection())
{
connection.Open();
var cmd = connection.CreateCommand();
cmd.CommandText = "[dbo].[DUES_AUTO_PAYMENT]";
cmd.CommandType = CommandType.StoredProcedure;
DbParameter pPaymentAmount = cmd.CreateParameter();
pPaymentAmount.ParameterName = "@Name";
pPaymentAmount.DbType = DbType.String;
pPaymentAmount.Direction = ParameterDirection.Input;
pPaymentAmount.Value = "Tom";
cmd.Parameters.Add(pPaymentAmount);
try
{
var x = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
return Ok(ex.Message);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.