Syntax error in this method button C#?
I need your help, I can't find the error, it's an emergency, here is my method and a image of the error the error say "syntax error INSERT INTO statement" here is an image, how can I fix it, no matter how its the structure
http://img718.imageshack.us/img718/8864/erroruh.jpg
private void btnCronograma_Click(object sender, EventArgs e)
{
string connstring = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\amaury\\Documents\\TEC\\Septimo Semestre\\Administracion de proyectos de ingenieria de softwaere\\nuevo4\\nuevo\\Office\\Office\\Policias.accdb";
using (OleDbConnection conn = new OleDbConnection(connstring))
{
conn.Open();
string sql = "INSERT INTO IndicadorProyecto (idProyecto, idMes, meta, real) VALUES(@idProyecto , @idMes , @meta, @real)";
OleDbCommand cmd = new OleDbCommand(sql, conn);
foreach (DataGridViewRow row in dataGridView8.Rows)
{
DataGridViewComboBoxCell combo3 = row.Cells["idProyecto"] as DataGridViewComboBoxCell;
DataGridViewComboBoxCell combo4 = row.Cells["idMes"] as DataGridViewComboBoxCell;
if (combo3 == null || combo4 == null)
{
MessageBox.Show("No se pudo convertir");
continue;
}
int idProyecto = int.Parse(combo3.Value.ToString());
int idMes = int.Parse(combo4.Value.ToString());
int meta = int.Parse(row.Cells[3].Value.ToString());
int real = int.Parse(row.Cells[4].Value.ToString());
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@idProyecto", idProyecto);
cmd.Parameters.AddWithValue("@idMes", idMes);
开发者_Go百科cmd.Parameters.AddWithValue("@meta", meta);
cmd.Parameters.AddWithValue("@real", real);
cmd.ExecuteNonQuery();
}
}
}
The error message is clear: Error in INSERT INTO statement.
I don't seen any SQL syntax problem so I'll guess it is about a reserve word, try escaping them with []
:
string sql = "INSERT INTO IndicadorProyecto (idProyecto, idMes, [meta], [real])
VALUES(@idProyecto , @idMes , @meta, @real)";
Combined with Remou's observation of named parameters it becomes:
string sql = "INSERT INTO IndicadorProyecto (idProyecto, idMes, [meta], [real])
VALUES(?, ?, ?, ?)";
And you have to be very careful about adding the parameters in the right order.
As you are using OLEDB provider, so probably you will have to used the "?" symbol for the parameters. You can try the following query:
string sql = "INSERT INTO IndicadorProyecto (idProyecto, idMes, [meta], [real])
VALUES(?, ? , ?, ?)";
精彩评论