数据库错误:“没有为一个或多个所需参数给出值". [英] a database error :"No value given for one or more required parameters."

查看:66
本文介绍了数据库错误:“没有为一个或多个所需参数给出值".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据网格,我应该将其列值插入到访问数据库中,但是command.ExecuteNonQuery();

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();

推荐答案

如以上注释之一所述,您应该首先更改代码以使用参数化查询.这将使您免于定界值的需要,并使代码更安全.另外,您应该利用using语句来使.NET更好地管理资源.

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();

如果进行这些修订后仍然收到错误,请再次检查INSERT语句中的字段名称.

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

这篇关于数据库错误:“没有为一个或多个所需参数给出值".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆