c# - 我当前的插入语句不起作用
问题描述
我正在为用户创建一个礼物表格来输入他们的价值观。然而,表格曾经可以工作,但突然,一天后,它就直接失效了。我不确定哪里出错了。请帮帮我。这是我的代码。我已经对其进行了审查,对我来说似乎很好。这个问题不断发生,我无法确定错误是什么。我认为这与日期有关。在数据库中,我将数据类型设置为 DATE。
CreateGift.cshtml
@*Create Gift Page*@
@model SignUp3.Models.Gift;
@section MoreScripts {
<script src="~/lib/moment/moment.min.js"></script>
<link href="~/lib/dtpicker/css/tempusdominus-bootstrap-4.min.css" rel="stylesheet" />
<script src="~/lib/dtpicker/js/tempusdominus-bootstrap-4.min.js"></script>
<link href="~/lib/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
<script src="~/lib/jquery-validate/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
}
<!DOCTYPE html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="~/lib/styles/createform.css">
</head>
<body>
<div class="center">
<form id="createuser" asp-controller="Gift" asp-action="CreateGift"
method="post">
<h3>Create Gift</h3>
<div class="form-group row">
<label class="control-label col-sm-3" asp-for="description">Description:<span style="color:red;">*</span> </label>
<div class="col-sm-5">
<input asp-for="description" placeholder="Full Name" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="description" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3" asp-for="vouchercode">Voucher Code:<span style="color:red;">*</span> </label>
<div class="col-sm-5">
<input asp-for="vouchercode" placeholder="Voucher Code" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="vouchercode" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3" asp-for="amount">Voucher Amount:<span style="color:red;">*</span> </label>
<div class="col-sm-5">
<input asp-for="amount" placeholder="Amount" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="amount" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3" asp-for="quantity">Quantity:<span style="color:red;">*</span> </label>
<div class="col-sm-5">
<input asp-for="quantity" placeholder="Quantity" class="form-control" />
</div>
<div class="col-sm-3">
<span asp-validation-for="quantity" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3" asp-for="activation_date">Activation Date:<span style="color:red;">*</span></label>
<div class="col-sm-5">
<input asp-for="activation_date" asp-format="{0:yyyy-MM-dd}"
class="form-control" placeholder="YYYY-MM-DD" />
</div>
<div class="col-sm-3">
<span asp-validation-for="activation_date" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<label class="control-label col-sm-3" asp-for="expiry">Expiry Date:<span style="color:red;">*</span></label>
<div class="col-sm-5">
<input asp-for="expiry" asp-format="{0:yyyy-MM-dd}"
class="form-control" placeholder="YYYY-MM-DD" />
</div>
<div class="col-sm-3">
<span asp-validation-for="expiry" class="text-danger"></span>
</div>
</div>
<div class="form-group row">
<div class="offset-sm-3 col-sm-6">
<input type="submit" value="Submit" class="btn btn-primary" />
</div>
</div>
@if (ViewData["Message"] != null)
{
<div class="form-group row">
<div class="offset-sm-2 col-sm-6">
<div class="alert alert-@ViewData["MsgType"]">
@Html.Raw(ViewData["Message"])
</div>
</div>
</div>
}
</form>
</div>
礼品控制器.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SignUp3.Models;
namespace SignUp3.Controllers
{
public class GiftController : Controller
{
[Authorize]
public IActionResult CreateGift()
{
return View();
}
[HttpPost]
public IActionResult CreateGift(Gift gift)
{
// TODO: L09 Task 5 - Write secure code to insert Users into database
if (!ModelState.IsValid)
{
ViewData["Message"] = "Invalid Input";
ViewData["MsgType"] = "warning";
return View("CreateGift");
}
else
{
string sql = @"INSERT INTO GiftVoucher (description,amount,vouchercode,quantity,activation_date,expiry)
VALUES('{0}',{1},'{2}',{3},'{4}','{5}')";
if (DBUtl.ExecSQL(sql, gift.description,gift.amount, gift.vouchercode,gift.quantity,gift.activation_date,gift.expiry) == 1)
{
TempData["Message"] = "Gift Sucessfully Added.";
TempData["MsgType"] = "success";
return RedirectToAction("ViewGift");
}
else
{
ViewData["Message"] = "Invalid";
ViewData["MsgType"] = "danger";
return View("CreateGift");
}
}
}
解决方案
从此字符串转换日期时转换失败17/12/2020 12:00:00 am
。
您应该格式化插入参数以满足数据类型date
gift.expiry.ToString("yyyy-MM-dd")