C# MVC ADO.NET在插入值时预期参数错误
问题消息:消息“参数化查询”(@ProductName nvarchar(3)、@SupplierID nvarchar(4000)、@CategoryID)需要未提供的参数“@SupplierID”。字符串 我一直在尝试使用MVC操作将多个值插入到经典North wind表的产品表中。 列出该表时没有任何问题,但当我尝试使用scaffold-ed-Create-page(自动生成)插入值时,上面给出了错误 这是我的ProductController和AddProduct cshtml 控制器C# MVC ADO.NET在插入值时预期参数错误,c#,model-view-controller,insert,ado.net,C#,Model View Controller,Insert,Ado.net,问题消息:消息“参数化查询”(@ProductName nvarchar(3)、@SupplierID nvarchar(4000)、@CategoryID)需要未提供的参数“@SupplierID”。字符串 我一直在尝试使用MVC操作将多个值插入到经典North wind表的产品表中。 列出该表时没有任何问题,但当我尝试使用scaffold-ed-Create-page(自动生成)插入值时,上面给出了错误 这是我的ProductController和AddProduct cshtml 控制器
public class ProductController : Controller
{
public string connectionString = @"Data Source = DESKTOP-O5J7OII\SQLEXPRESS01; Initial Catalog = NORTHWND; Integrated Security = True";
//NORTHWNDEntities Kod = new NORTHWNDEntities();
//Product x = new Product();
public ActionResult Index(Product t = null)
{
DataTable tableProduct = new DataTable();
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter("use NORTHWND SELECT * FROM Products Order By ProductID ASC", sqlCon);
sqlDA.Fill(tableProduct);
}
return View(tableProduct);
//var result = Kod.Products.Where(x => x.ProductID > 0).OrderBy(x => x.ProductID).ToList();
//return View(result.ToList());
}
public ActionResult AddProduct()
{
return View(new WebApplication1.Product());
}
[HttpPost]
public ActionResult AddProduct(Product productModel)
{
using (SqlConnection sqlCon = new SqlConnection(connectionString))
{
sqlCon.Open();
string query = @"INSERT INTO Products VALUES(@ProductName,@SupplierID,@CategoryID,@QuantityPerUnit,@UnitPrice,@UnitsInStock,@UnitsOnOrder,@ReorderLevel)";
SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
sqlCmd.Parameters.AddWithValue("@ProductName", productModel.ProductName);
sqlCmd.Parameters.AddWithValue("@SupplierID", productModel.SupplierID);
sqlCmd.Parameters.AddWithValue("@CategoryID", productModel.CategoryID);
sqlCmd.Parameters.AddWithValue("@QuantityPerUnit", productModel.QuantityPerUnit);
sqlCmd.Parameters.AddWithValue("@UnitPrice", productModel.UnitPrice);
sqlCmd.Parameters.AddWithValue("@UnitsInStock", productModel.UnitsInStock);
sqlCmd.Parameters.AddWithValue("@UnitsOnOrder", productModel.UnitsOnOrder);
sqlCmd.Parameters.AddWithValue("@ReorderLevel", productModel.ReorderLevel);
sqlCmd.Parameters.AddWithValue("@Discontinued", productModel.Discontinued);
sqlCmd.ExecuteNonQuery();
}
return RedirectToAction("Index");
}
和.cshtml
@model WebApplication1.Product
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Product</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ProductName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.QuantityPerUnit, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.QuantityPerUnit, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.QuantityPerUnit, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UnitPrice, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UnitPrice, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UnitPrice, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Discontinued, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="checkbox">
@Html.EditorFor(model => model.Discontinued)
@Html.ValidationMessageFor(model => model.Discontinued, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@model WebApplication1.Product
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()
产品
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(model=>model.ProductName,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.ProductName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.ProductName,“,新{@class=“text danger”})
@LabelFor(model=>model.QuantityPerUnit,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.quantitypernit,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.QuantityPerUnit,“,new{@class=“text danger”})
@LabelFor(model=>model.UnitPrice,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.UnitPrice,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.UnitPrice,“,new{@class=“text danger”})
@LabelFor(model=>model.contracted,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.contracted)
@Html.ValidationMessageFor(model=>model.contracted,“,new{@class=“text danger”})
}
@ActionLink(“返回列表”、“索引”)
感谢您的帮助并回复!:)第一个问题是您提供的参数(9)超过了查询需要的参数(8)<代码>@不连续不是必需的 错误表示未提供
@SupplierID
。这意味着它有null
值。检查db是否该列支持null
值。然后将该行更改如下:
sqlCmd.Parameters.AddWithValue("@SupplierID", productModel.SupplierID ?? DBNull.Value);
???为空合并运算符。建议使用存储过程:
CREATE PROC AddProduct
@ProductName nvarchar(100),
@SupplierID int=null, /*This line inserts a DBNull, if parameter is null*/
@CategoryID int,
@QuantityPerUnit int,
@UnitPrice money,
@UnitsInStock int,
@UnitsOnOrder int,
@ReorderLevel int
BEGIN
INSERT INTO Products VALUES(@ProductName,@SupplierID,@CategoryID,@QuantityPerUnit,@UnitPrice,@UnitsInStock,@UnitsOnOrder,@ReorderLevel)
END
第一个问题是您提供的参数(9)比查询需要的参数(8)多<代码>@不连续不是必需的 错误表示未提供
@SupplierID
。这意味着它有null
值。检查db是否该列支持null
值。然后将该行更改如下:
sqlCmd.Parameters.AddWithValue("@SupplierID", productModel.SupplierID ?? DBNull.Value);
???为空合并运算符。建议使用存储过程:
CREATE PROC AddProduct
@ProductName nvarchar(100),
@SupplierID int=null, /*This line inserts a DBNull, if parameter is null*/
@CategoryID int,
@QuantityPerUnit int,
@UnitPrice money,
@UnitsInStock int,
@UnitsOnOrder int,
@ReorderLevel int
BEGIN
INSERT INTO Products VALUES(@ProductName,@SupplierID,@CategoryID,@QuantityPerUnit,@UnitPrice,@UnitsInStock,@UnitsOnOrder,@ReorderLevel)
END