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

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

Oledb ExecuteNonQuery function is not inserting all data into database

问题

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

我尝试在C#中将值插入Access数据库,但只有部分数据被插入。数据库中的所有数据类型和C#代码都是正确的。

下面的代码会引发错误:

> INSERT INTO语句中的语法错误

代码:

string insertStatement =
    "INSERT INTO Car (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location, Image, Manufacturer, Description) " +
    "VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location, @Image, @Manufacturer, @Description)";

OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
OleDbCommand command = new OleDbCommand(insertStatement, connection);

command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@CarYear", car.year);
command.Parameters.AddWithValue("@Mileage", car.mileage);
command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
command.Parameters.AddWithValue("@Availability", car.availability);
command.Parameters.AddWithValue("@Location", car.location);
command.Parameters.AddWithValue("@Image", car.image);
command.Parameters.AddWithValue("@Manufacturer", car.manufacturer);
command.Parameters.AddWithValue("@Description", car.description);

connection.Open();

command.ExecuteNonQuery();

然而,如果我去掉ImageManufacturer和Description,那么它就会执行而不会出现错误。以下是不返回错误并只插入部分数据的代码:

string insertStatement =
    "INSERT INTO CAR (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location)" +
    " VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location)";

OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
OleDbCommand command = new OleDbCommand(insertStatement, connection);

command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@CarYear", car.year);
command.Parameters.AddWithValue("@Mileage", car.mileage);
command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
command.Parameters.AddWithValue("@Availability", car.availability);
command.Parameters.AddWithValue("@Location", car.location);

connection.Open();

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:

string insertStatement =
             "INSERT INTO Car (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location, Image, Manufacturer, Description) " +
             "VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location, @Image, @Manufacturer, @Description)";

OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
OleDbCommand command = new OleDbCommand(insertStatement, connection);

command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@CarYear", car.year);
command.Parameters.AddWithValue("@Mileage", car.mileage);
command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
command.Parameters.AddWithValue("@Availability", car.availability);
command.Parameters.AddWithValue("@Location", car.location);
command.Parameters.AddWithValue("@Image", car.image);
command.Parameters.AddWithValue("@Manufacturer", car.manufacturer);
command.Parameters.AddWithValue("@Description", car.description);

connection.Open();

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:

string insertStatement = 
           "INSERT INTO CAR (Category, Model, CarYear, Mileage, CostPerDay, Availability, Location)" +
           " VALUES (@Category, @Model, @CarYear, @Mileage, @CostPerDay, @Availability, @Location)";

OleDbConnection connection = new OleDbConnection(CarRentalDB.ConnectionString);
OleDbDataAdapter adapter = new OleDbDataAdapter(insertStatement, connection);
OleDbCommand command = new OleDbCommand(insertStatement, connection);

command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@Category", car.category.ToString());
command.Parameters.AddWithValue("@Model", car.model);
command.Parameters.AddWithValue("@CarYear", car.year);
command.Parameters.AddWithValue("@Mileage", car.mileage);
command.Parameters.AddWithValue("@CostPerDay", car.costPerDay);
command.Parameters.AddWithValue("@Availability", car.availability);
command.Parameters.AddWithValue("@Location", car.location);

connection.Open();

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,根据您的解释。请将这些标识符用方括号括起来:

, [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.

答案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:

确定