在ASP.Net Core 2.2中运行存储过程

时间:2019-07-11 04:19:47

标签: c# asp.net-core asp.net-web-api

为什么我的asp.net网络api无法识别此代码行

db.Database.Initialize(force: false);
var connection = db.Database.Connection

它可以在我的桌面应用程序中工作,但不能在我的webapi中工作。我已经安装了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);
    }
}

您能帮我吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

对于正在运行的过程,可以通过Database.GetDbConnection()获得连接。

尝试以下操作:

    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);
                }
            }            
        }       
    }