简体   繁体   English

Access数据库C#中的更新查询错误

[英]Error with update query in a Access database c#

I have an error in my code. 我的代码有错误。 It gives me an OleDbException (No value given for one or more required parameter), hope someone can show the the way. 它给了我一个OleDbException(没有为一个或多个必需的参数给出值),希望有人能提供帮助。

here is part of my code: 这是我的代码的一部分:

public void buttonEditar_Click(object sender, EventArgs e)
{
   OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Gera\Documents\Visual Studio 2012\Projects\RichyAutoCenter\RichyAutoCenter\bin\Debug\Camionetas.accdb;");

   string query = "UPDATE [Camionetas] SET VIN = ?, Marca = ?, Modelo = ?, Año = ?, Rentada = ?, FechaRenta = ?, FechaEntrega = ?, DiasAtrasados = ?, FechaAceite = ?, FechaCambio = ?, Obervaciones = ?, PersonaRentada = ? WHERE ID = ?";

   int ID = int.Parse(numero);
   string Vin = textBoxVin.Text;
   string Marca = textBoxMarca.Text;
   string Modelo = textBoxModelo.Text;
   string Ano = textBoxAno.Text;
   Boolean Rentada = checkBoxRentada.Checked;
   string FechaRenta = dateTimePickerFechaRenta.Value.ToShortDateString();
   string FechaEntrega = dateTimePickerFechaEntrega.Value.ToShortDateString();
   int DiasAtrasados = int.Parse(textBoxDiasAtrasados.Text);
   string FechaAceite = dateTimePickerFechaAceite.Value.ToShortDateString();
   string FechaCambio = dateTimePickerFechaCambio.Value.ToShortDateString();
   string Observaciones = textBoxObservaciones.Text;
   string PersonaRentada = comboBoxPersonaRenta.Text;

   OleDbCommand command = new OleDbCommand(query, connection);
   command.CommandType = CommandType.Text;

   command.Parameters.AddWithValue("?",Vin);
   command.Parameters.AddWithValue("?",Marca);
   command.Parameters.AddWithValue("?",Modelo);
   command.Parameters.AddWithValue("?",Ano);
   command.Parameters.AddWithValue("?",Rentada);
   command.Parameters.AddWithValue("?",FechaRenta);
   command.Parameters.AddWithValue("?",FechaEntrega);
   command.Parameters.AddWithValue("?",DiasAtrasados);
   command.Parameters.AddWithValue("?",FechaAceite);
   command.Parameters.AddWithValue("?",FechaCambio);
   command.Parameters.AddWithValue("?",Observaciones);
   command.Parameters.AddWithValue("?",PersonaRentada);
   command.Parameters.AddWithValue("?",ID);

   connection.Open();
   command.ExecuteNonQuery(); // No value given for one or more required parameters error
   connection.Close();
}

replace all yours command.Parameters.AddWithValue("?",ID); 替换所有您的command.Parameters.AddWithValue("?",ID); with general: command.Parameters.Add(ID); 与一般: command.Parameters.Add(ID);

or use different names instead of ? 或使用其他名称代替? , check out examples at http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx ,请在http://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx中查看示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM