缺少参数。 [参数序数= 1]

时间:2014-05-04 10:42:11

标签: c# html sql parameters sql-insert

我想出错误,"缺少参数。当我尝试运行以下SQL查询时,在Web Matrix中[参数序号= 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],[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(更新)行上,但是,我不确定我的附加参数应该是什么。

客户是我的表,它包含大写标题(例如Customer_First_Name),然后我想要插入的值是非大写的(例如customer_first_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>

列如下:

enter image description here 感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

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

如果您错过了一个库:添加using System.Data.SqlClient;

我希望它能帮到你