1

I have a data grid that I should insert its columns values to an access database but I have problem with command.ExecuteNonQuery();

My project is not finished just because of this error. Here is my code :

for (int i = 0; i < (dataGridFactorRent.Rows.Count) - 1; i++)
{
    string query =
        @"INSERT INTO tbl_RentFactor([ID],DateNow,customerName, objectName, 
          objectNumber,unitCost,objectCost,paidMoney,restOfMonyy,customerID,DateBack)
          VALUES ("+ID+",'" + lbldate.Text + "','" + cmdCustomName.Text + "'," +
              dataGridFactorRent.Rows[i].Cells[1].Value + ",
              " + dataGridFactorRent.Rows[i].Cells[3].Value + ",
              " + dataGridFactorRent.Rows[i].Cells[4].Value + ",
              " + dataGridFactorRent.Rows[i].Cells[5].Value + ",
              '" + txtPaid.Text + "','" + lblRemained.Text + "',
              "+customerID+",'"+lbldate.Text+"')";

    con.Open();
    command.CommandText =query;
    command.ExecuteNonQuery();
    con.Close();
4

2 回答 2

1

As suggested in one of the comments above, you should start by changing your code to use a parameterized query. That will relieve you of the need to delimit values, and will also make your code safer. In addition, you should take advantage of the using statement to let .NET manage resources better.

After making those changes your code would look more like this:

string query =
    @"INSERT INTO tbl_RentFactor([ID],DateNow,customerName, objectName, 
      objectNumber,unitCost,objectCost,paidMoney,restOfMonyy,customerID,DateBack)
      VALUES (?,?,?,?,?,?,?,?,?,?,?)";
con.Open();
for (int i = 0; i < (dataGridFactorRent.Rows.Count) - 1; i++)
{
    using (var command = new OleDbCommand(query, con));
    {
        command.Parameters.AddWithValue("?", ID);
        command.Parameters.AddWithValue("?", lbldate.Text);
        command.Parameters.AddWithValue("?", cmdCustomName.Text);
        command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[1].Value);
        command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[3].Value);
        command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[4].Value);
        command.Parameters.AddWithValue("?", dataGridFactorRent.Rows[i].Cells[5].Value);
        command.Parameters.AddWithValue("?", txtPaid.Text);
        command.Parameters.AddWithValue("?", lblRemained.Text);
        command.Parameters.AddWithValue("?", customerID);
        command.Parameters.AddWithValue("?", lbldate.Text);

        command.ExecuteNonQuery();
    }
}
con.Close();

If you still receive an error after making those revisions then double-check the field names in your INSERT statement.

于 2013-06-02T09:53:24.920 回答
0

It means a column is not found in the table, (so Access thinks it's a parameter). Usually you misspelled something. It seems likely "restOfMonyy" should be "restOfMoney". If not, debug the application and get the exact string built and make a query with it and see what happens.

于 2013-06-02T09:30:26.473 回答