Oledb ExecuteNonQuery函数未将所有数据插入到数据库中。

huangapple go评论94阅读模式
英文:

Oledb ExecuteNonQuery function is not inserting all data into database

问题

以下是您提供的代码部分的翻译:

  1. 我尝试在C#中将值插入Access数据库,但只有部分数据被插入。数据库中的所有数据类型和C#代码都是正确的。
  2. 下面的代码会引发错误:
  3. > INSERT INTO语句中的语法错误
  4. 代码:
  5. string insertStatement =
  6. "INSERT INTO Car (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location, Image, Manufacturer, Description) " +
  7. "VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location, @Image, @Manufacturer, @Description)";
  8. OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
  9. OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
  10. OleDbCommand command = new OleDbCommand(insertStatement, connection);
  11. command.Parameters.AddWithValue("@Category", car.category.ToString());
  12. command.Parameters.AddWithValue("@Model", car.model);
  13. command.Parameters.AddWithValue("@CarYear", car.year);
  14. command.Parameters.AddWithValue("@Mileage", car.mileage);
  15. command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
  16. command.Parameters.AddWithValue("@Availability", car.availability);
  17. command.Parameters.AddWithValue("@Location", car.location);
  18. command.Parameters.AddWithValue("@Image", car.image);
  19. command.Parameters.AddWithValue("@Manufacturer", car.manufacturer);
  20. command.Parameters.AddWithValue("@Description", car.description);
  21. connection.Open();
  22. command.ExecuteNonQuery();
  23. 然而,如果我去掉ImageManufacturerDescription,那么它就会执行而不会出现错误。以下是不返回错误并只插入部分数据的代码:
  24. string insertStatement =
  25. "INSERT INTO CAR (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location)" +
  26. " VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location)";
  27. OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
  28. OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
  29. OleDbCommand command = new OleDbCommand(insertStatement, connection);
  30. command.Parameters.AddWithValue("@Category", car.category.ToString());
  31. command.Parameters.AddWithValue("@Model", car.model);
  32. command.Parameters.AddWithValue("@CarYear", car.year);
  33. command.Parameters.AddWithValue("@Mileage", car.mileage);
  34. command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
  35. command.Parameters.AddWithValue("@Availability", car.availability);
  36. command.Parameters.AddWithValue("@Location", car.location);
  37. connection.Open();
  38. command.ExecuteNonQuery();

请注意,这些翻译部分仅包括您提供的代码和错误消息,没有其他内容。如果您需要更多帮助,请随时提问。

英文:

I'm trying to insert values into an Access database with C#, but only some of the data is being inserted. All of the data types in the database and the C# code are correct.

This code below throws an error:

> Syntax ERROR in INSERT INTO statement

Code:

  1. string insertStatement =
  2. "INSERT INTO Car (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location, Image, Manufacturer, Description) " +
  3. "VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location, @Image, @Manufacturer, @Description)";
  4. OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
  5. OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
  6. OleDbCommand command = new OleDbCommand(insertStatement, connection);
  7. command.Parameters.AddWithValue("@Category", car.category.ToString());
  8. command.Parameters.AddWithValue("@Model", car.model);
  9. command.Parameters.AddWithValue("@CarYear", car.year);
  10. command.Parameters.AddWithValue("@Mileage", car.mileage);
  11. command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
  12. command.Parameters.AddWithValue("@Availability", car.availability);
  13. command.Parameters.AddWithValue("@Location", car.location);
  14. command.Parameters.AddWithValue("@Image", car.image);
  15. command.Parameters.AddWithValue("@Manufacturer", car.manufacturer);
  16. command.Parameters.AddWithValue("@Description", car.description);
  17. connection.Open();
  18. command.ExecuteNonQuery();

However, if I get rid of the Image, Manufacturer, and Description then it executes with no errors. Here is the code that does not return errors and inserts only some of the data:

  1. string insertStatement =
  2. "INSERT INTO CAR (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location)" +
  3. " VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location)";
  4. OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
  5. OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
  6. OleDbCommand command = new OleDbCommand(insertStatement, connection);
  7. command.Parameters.AddWithValue("@Category", car.category.ToString());
  8. command.Parameters.AddWithValue("@Model", car.model);
  9. command.Parameters.AddWithValue("@Category", car.category.ToString());
  10. command.Parameters.AddWithValue("@Model", car.model);
  11. command.Parameters.AddWithValue("@CarYear", car.year);
  12. command.Parameters.AddWithValue("@Mileage", car.mileage);
  13. command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
  14. command.Parameters.AddWithValue("@Availability", car.availability);
  15. command.Parameters.AddWithValue("@Location", car.location);
  16. connection.Open();
  17. command.ExecuteNonQuery();

Here is what the data looks like (excuse the multiple entries of the same data)

Thank you for your time!

答案1

得分: 1

如果出现语法错误,那几乎肯定是因为您将保留字用作标识符。我敢肯定,如果您查找一下Access保留字的列表,您可能会找到Image和/或Description,根据您的解释。请将这些标识符用方括号括起来:

  1. , [Image], Manufacturer, [Description])

然后您应该可以正常运行。您可以逐个尝试它们,看看是否只是其中一个出了问题 - Image几乎肯定会有问题,但Description就不太确定。请注意,您始终可以将所有标识符都括在方括号中,这样就不会出现此问题。

英文:

If there's a syntax error then that's almost certainly because you are using a reserved word as an identifier. I'm sure you can find a list of Access reserved words if you look but it's likely Image and/or Description, based on your explanation. Wrap those identifiers in brackets

  1. , [Image], Manufacturer, [Description])

and you should be good to go. You can try each one individually to see if it's just one of them - Image is almost certainly a problem but not sure about Description. Note that you can always just wrap all your identifiers in brackets and you'll never have this issue.

答案2

得分: 0

如果出现语法错误,那几乎肯定是因为您将保留字用作标识符。我确定您可以找到Access保留字列表,但根据您的解释,很可能是Image和/或Description。将这些标识符括在方括号中([Image],Manufacturer,[Description]),然后您应该可以继续。您可以逐个尝试它们,以查看是否只有其中一个出现问题 - Image几乎肯定会有问题,但Description就不太确定。请注意,您始终可以将所有标识符都括在方括号中,这样您将永远不会遇到此问题。

英文:

If there's a syntax error then that's almost certainly because you are using a reserved word as an identifier. I'm sure you can find a list of Access reserved words if you look but it's likely Image and/or Description, based on your explanation. Wrap those identifiers in brackets

, [Image], Manufacturer, [Description])
and you should be good to go. You can try each one individually to see if it's just one of them - Image is almost certainly a problem but not sure about Description. Note that you can always just wrap all your identifiers in brackets and you'll never have this issue.

huangapple
  • 本文由 发表于 2023年4月4日 09:53:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924938.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定