C# 数据库错误:";没有为一个或多个所需参数提供值。”;

C# 数据库错误:";没有为一个或多个所需参数提供值。”;,c#,ms-access,ms-access-2007,C#,Ms Access,Ms Access 2007,我有一个数据网格,我应该将它的列值插入access数据库,但我在command.ExecuteNonQuery()方面有问题 因为这个错误,我的项目没有完成。这是我的密码: for (int i = 0; i < (dataGridFactorRent.Rows.Count) - 1; i++) { string query = @"INSERT INTO tbl_RentFactor([ID],DateNow,customerName, objectName,

我有一个数据网格,我应该将它的列值插入access数据库,但我在
command.ExecuteNonQuery()方面有问题

因为这个错误,我的项目没有完成。这是我的密码:

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();
for(int i=0;i<(dataGridFactorRent.Rows.Count)-1;i++)
{
字符串查询=
@“插入tbl_RentFactor([ID]、DateNow、customerName、objectName、,
objectNumber、unitCost、objectCost、paidMoney、RestofMoney、customerID、DateBack)
值(“+ID+”、“+lbldate.Text+”、“+cmdCustomName.Text+”、”+
DataGridFactoryCurrent.Rows[i].单元格[1].值+“,
“+DataGridFactoryCurrent.Rows[i].单元格[3].值+”,
“+DataGridFactoryCurrent.Rows[i]。单元格[4]。值+”,
“+DataGridFactoryCurrent.Rows[i]。单元格[5]。值+”,
“+txtpayed.Text+”,“+lblremaine.Text+”,
“+customerID+”,“+lbldate.Text+”)”;
con.Open();
command.CommandText=查询;
command.ExecuteNonQuery();
con.Close();

这意味着在表中找不到列(因此Access认为它是一个参数)。通常您拼写错误。似乎“restofmony”应该是“restOfMoney”。如果不是,则调试应用程序并生成准确的字符串,并使用它进行查询,然后查看发生了什么情况。

这意味着在表中找不到列,(所以Access认为这是一个参数)。通常您拼错了某些内容。似乎“restofmony”应该是“restOfMoney”。如果没有,请调试应用程序并生成准确的字符串,然后使用它进行查询,看看会发生什么。

正如上面的一条评论中所建议的,您应该从更改代码开始,使用参数化查询。这将使您不再需要定界值,也将使代码更安全。此外,您应该使用adv
使用
语句让.NET更好地管理资源的缺点

进行这些更改后,您的代码看起来更像这样:

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();
字符串查询=
@“插入tbl_RentFactor([ID]、DateNow、customerName、objectName、,
objectNumber、unitCost、objectCost、paidMoney、RestofMoney、customerID、DateBack)
值(?,,,,,,,,,,,,,,,,,,?)”;
con.Open();
对于(int i=0;i<(dataGridFactorRent.Rows.Count)-1;i++)
{
使用(var命令=新的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(“?”,txtpayed.Text);
command.Parameters.AddWithValue(“?”,lblRemained.Text);
command.Parameters.AddWithValue(“?”,customerID);
command.Parameters.AddWithValue(“?”,lbldate.Text);
command.ExecuteNonQuery();
}
}
con.Close();

如果在进行这些修改后仍然收到错误,请仔细检查INSERT语句中的字段名。

正如上面一条评论中所建议的,您应该首先更改代码以使用参数化查询。这将免除您对值进行定界的需要,也将使您的代码更加安全。此外,您应该uld利用
使用
语句让.NET更好地管理资源

进行这些更改后,您的代码看起来更像这样:

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();
字符串查询=
@“插入tbl_RentFactor([ID]、DateNow、customerName、objectName、,
objectNumber、unitCost、objectCost、paidMoney、RestofMoney、customerID、DateBack)
值(?,,,,,,,,,,,,,,,,,,?)”;
con.Open();
对于(int i=0;i<(dataGridFactorRent.Rows.Count)-1;i++)
{
使用(var命令=新的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(“?”,txtpayed.Text);
command.Parameters.AddWithValue(“?”,lblRemained.Text);
command.Parameters.AddWithValue(“?”,customerID);
command.Parameters.AddWithValue(“?”,lbldate.Text);
command.ExecuteNonQuery();
}
}
con.Close();

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

ExecuteNonQuery
之前调试并设置断点,然后检查
query
。在数据库上手动测试。避免这些错误,您可以使用参数。在
ExecuteNonQue之前调试并设置断点ry
并检查
查询
。在数据库上手动测试。避免这些错误,您可以使用参数。