2010-11-21 12 views
0

Access (.accdb) ve C# ile yazılmış bir Windows Forms uygulamasıyla çalışıyorum.
Veritabanına veri eklemek istediğimde bu hatayı alıyorum.Access INSERT deyiminde ve .NET DataGridView denetiminde bir sözdizimi hatası nasıl kullanılır?

System.Data.OleDb.OleDbException: INSERT INTO deyimindeki Sintax hatası. adaptador.InsertCommand.ExecuteNonQuery();

Doğrudan rotadan ve güncelleme yönteminden geçmeye çalıştım ama aynı hatayla geliyor!

Kodum:

private void btnCronograma_Click(object sender, EventArgs e) 
{ 
    OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=mydatabase.accdb"); 
    string sql; 
    int idProyecto, idMes, meta, real; 

    OleDbDataAdapter adaptador = new OleDbDataAdapter(); 

    //for (int i = 0; i < this.dataGridView8.Rows.Count - 1; i++) 
    //{ 
    foreach (DataGridViewRow row in dataGridView8.Rows) 
    { 
     DataGridViewComboBoxCell combo3 = row.Cells["idProyecto"] as DataGridViewComboBoxCell; 
     DataGridViewComboBoxCell combo4 = row.Cells["idMes"] as DataGridViewComboBoxCell; 

     MessageBox.Show(combo3.Value.ToString()); 
     MessageBox.Show(combo4.Value.ToString()); 

     idProyecto = int.Parse(combo3.Value.ToString()); 
     idMes = int.Parse(combo4.Value.ToString()); 

     meta = int.Parse(dataGridView8.Rows[0].Cells[3].Value.ToString()); 
     real = int.Parse(dataGridView8.Rows[0].Cells[4].Value.ToString()); 
     MessageBox.Show(meta.ToString()); 
     MessageBox.Show(real.ToString()); 

     //for (int i = 0; i < this.dataGridView8.Rows.Count - 1; i++) 
     //{ 
     sql = "INSERT INTO IndicadorProyecto (idProyecto, idMes, meta, real) VALUES('" + idProyecto + "' , '" + 
       idMes + "' , '" + meta + "' , '" + real + "') "; 

     //sql = "INSERT INTO IndicadorProyecto (idMes, meta, real) VALUES('" + idMes + "' , '" + meta + "' , '" + real + "') "; 

     if (combo3 == null) 
     { 
      MessageBox.Show("No se pudo convertir"); 
     } 
     else if (combo4 == null) 
     { 
      MessageBox.Show("No se pudo convertir"); 
     } 
     else 
     { 

     } 

     try 
     { 
      conn.Open(); 
      adaptador.InsertCommand = new OleDbCommand(sql, conn); 
      adaptador.InsertCommand.ExecuteNonQuery(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.ToString()); 
     } 
     finally 
     { 
      conn.Close(); 
     } 
     //} 
    } 
} 

cevap

2

SQL ifadeleri dizeleri bitiştirmek olmamalıdır.
Bunu yaparak, kodunuzu kırıp bir SQL püskürtme güvenlik açığı oluşturursunuz.

Bunun yerine, parametreleri kullanmalısınız. Örneğin

:

using (var conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=mydatabase.accdb")) 
using (var command = new OleDbCommand(@"INSERT INTO IndicadorProyecto (idProyecto, idMes, meta, real) VALUES(?, ?, ?, ?") { 
    command.Parameters.AddWithValue("a", idProyecto); 
    command.Parameters.AddWithValue("b", idMes); 
    command.Parameters.AddWithValue("c", meta); 
    command.Parameters.AddWithValue("d", real); 

    conn.Open(); 
    command.ExecuteNonQuery(); 
} 
+1

Hep kabul edilebilir olması için dizeleri bitiştirmek kabul ettik ** Eğer mesela, bir seçenek seçim için dayalı dizeleri kontrolü sizdedir ** eğer. – RolandTumble

İlgili konular