尝试使用ASP.NET将数据写入数据库-错误“;必须声明标量变量“”;不确定原因
本文关键字:声明 标量 变量 不确定 错误 ASP NET 数据库 数据 | 更新日期: 2023-09-27 17:57:28
我是一个新手用户,这是我第一次使用这个系统。刚才只是想做一个非常简单的功能,包括将数据输入表单,按下提交按钮,然后将值存储在我的数据库中。有人看到我哪里出错了吗?我得到错误
必须声明标量变量@forename
@{
// Initialize variables
var forename = "";
var surname = "";
var email = "";
var street = "";
var gender = "";
var towncity = "";
var postcode = "";
var cardno = "";
var expiry = "";
var securecode = "";
var password = "";
// If this is a POST request validate and process data
if (IsPost) {
forename = Request.Form["forename"];
surname = Request.Form["surname"];
gender = Request.Form["gender"];
street = Request.Form["street"];
towncity = Request.Form["townCity"];
postcode = Request.Form["postcode"];
cardno = Request.Form["cardno"];
expiry = Request.Form["expiry"];
securecode = Request.Form["securecode"];
password = Request.Form["password"];
email = Request.Form["email"];
var db = Database.Open("calcinema");
var cmd = "INSERT INTO Account (Forename, Surname, Email, Street, Gender, TownCity, Postcode, Cardnumber, Expiry, SecureCode, Password) VALUES (@forename, @surname, @email, @street, @gender, @towncity, @postcode, @cardno, @expiry, @securecode, @password)";
db.Execute(cmd, forename, surname, email, street, gender, towncity, postcode, cardno, expiry, securecode, password);
Response.Redirect("~/Browse");
}
}
这很可能是一件非常简单的事情,但我看不到它,我已经遵循了与相同结构的模板/教程
Webmatrix.Database
按索引而非名称映射查询中的参数。因此,与其使用@forename
,不如使用@0
,依此类推:
var cmd = "INSERT INTO Account (Forename, Surname, Email, Street, Gender, TownCity, Postcode, Cardnumber, Expiry, SecureCode, Password) VALUES (@0, @1, @2, @3, ...)";
代码的其他部分不应更改。也许这不是一个好方法,但WebMatrix没有提供其他选择。