[英]Error with update query in a Access database c#
我的代碼有錯誤。 它給了我一個OleDbException(沒有為一個或多個必需的參數給出值),希望有人能提供幫助。
這是我的代碼的一部分:
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();
}
替換所有您的command.Parameters.AddWithValue("?",ID);
與一般: command.Parameters.Add(ID);
或使用其他名稱代替?
,請在http://msdn.microsoft.com/zh-CN/library/system.data.sqlclient.sqlparametercollection.addwithvalue.aspx中查看示例
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.