C# 缺少一个参数。[参数序号=1]
当我尝试运行以下SQL查询时,在Web Matrix中出现错误“缺少参数。[parameter ordinal=1]”:C# 缺少一个参数。[参数序号=1],c#,html,sql,parameters,sql-insert,C#,Html,Sql,Parameters,Sql Insert,当我尝试运行以下SQL查询时,在Web Matrix中出现错误“缺少参数。[parameter ordinal=1]”: if(IsPost) { var db = Database.Open("TheatreBooking"); var update = "INSERT INTO Customer ([Customer_First_Name],[Customer_Surname],[Customer_Add_Ln_1], [Customer_Add_Ln_2],[Cus
if(IsPost)
{
var db = Database.Open("TheatreBooking");
var update =
"INSERT INTO Customer ([Customer_First_Name],[Customer_Surname],[Customer_Add_Ln_1],
[Customer_Add_Ln_2],[Customer_Add_Ln_3],[Customer_Add_Ln_4],[Customer_Postcode],
[Customer_Tel],[Customer_Email]) VALUES
(@customer_first_name,@customer_surname,@customer_add_ln_1,
@customer_add_ln_2,@customer_add_ln_3,@customer_add_ln_4,
@customer_postcode,@customer_telephone,@customer_email)";
db.Execute(update);
}
它似乎在db.execute(update)行,但是,我不确定我的附加参数应该是什么
Customer是我的表,它包括大写的标题(例如Customer\u First\u Name),然后我要插入的值是非大写的(例如Customer\u First\u Name),已输入到表格中:
<form action="" method ="post" >
<p>First Name</p>
<input type="text" name ="customer_first_name"> </input> </br>
<p>Surname</p>
<input type="text" name ="customer_surname"> </input> </br>
<p>Address</p>
<input type="text" name ="customer_add_ln_1"> </input> </br>
<input type="text" name ="customer_add_ln_2"> </input> </br>
<input type="text" name ="customer_add_ln_3"> </input> </br>
<input type="text" name ="customer_add_ln_4"> </input> </br>
<p>Postcode</p>
<input type="text" name ="customer_postcode"> </input> </br>
<p>Telephone</p>
<input type="text" name ="customer_telephone"> </input> </br>
<p>Email</p>
<input type="text" name ="customer_email"> </input> </br>
<input type="Submit" name ="Confirm"> </input>
</form>
名字
姓
地址
邮政编码
电话
电子邮件
各栏如下:
谢谢。您可以使用以下内容:
using (SqlConnection connection = new SqlConnection("TheatreBooking"))// I guess "TheatreBooking" is your connectionString
{
connection.Open();
using (SqlCommand command = connection.CreateCommand())
{
command.CommandText = "INSERT INTO Customer ([Customer_First_Name],[Customer_Surname],
[Customer_Add_Ln_1],[Customer_Add_Ln_2],[Customer_Add_Ln_3],
[Customer_Add_Ln_4],[Customer_Postcode], [Customer_Tel],[Customer_Email]) VALUES
(@customer_first_name,@customer_surname,@customer_add_ln_1,
@customer_add_ln_2,@customer_add_ln_3,@customer_add_ln_4,
@customer_postcode,@customer_telephone,@customer_email)";
command.Parameters.AddWithValue("@customer_first_name",customerfirstname));
command.Parameters.AddWithValue("@customer_surname",customersurname));
....// you do the same for the rest (6)as the above line
command.ExecuteNonQuery();
}
}
如果缺少库:使用System.Data.SqlClient添加代码>
我希望它能帮助您在哪里为@customer\u first\u name等提供值?@marcacierno它们是由用户在运行时以表单形式输入的-请参见上面的修改表的ID是否具有自动递增属性?或者在您的表中是否有其他列不能为空。您能显示表中的所有列吗?@lnanikian不幸的是,自动递增选项被禁用,因此我取消了Customer_ID(我有机地拥有),并将PK更改为Customer_Tel,因为用户不能使用与其他人相同的电话号码注册。我知道这很糟糕,但我能为这个大学项目找到的唯一快速解决方案!在哪里分配这些参数?您可以使用SqlCommand对象吗?如果您不确定是否使用db.Execute()
。