简体   繁体   中英

Syntax error (missing operator) in query expression, Oledb INSERT Statement

I know there's already a bunch of these posts but I looked through a lot of them and they all seem specific to one person's needs, so I figured I'd see if someone could help me for my specific needs.

I am using Oledb and c# and I am trying to create an Excel worksheet and populate it but I'm getting the error "Syntax error (missing operator) in query expression" when using this INSERT statement:

for (int i = 0; i <= 20; i++)
{
     command.CommandText = string.Format("INSERT INTO [Table] (Id, Tag, Description) VALUES ('{0}','2','3')", diff1[i]);
     command.ExecuteNonQuery();
}

To explain, I have an array of strings, diff1[], and I'm using the loop to insert each element into its own cell, and it works until it gets to the string element:

"\r\n\"603\",\"FuelPumpTestWarning\",\"<table style=\"\"border: 4px solid black; border-collapse:collapse;\"\">\r\n<tbody>\r\n<tr>\r\n<td bgcolor=\"\"#FF7700\"\" style=\"\"border: 5px solid black; border-collapse:collapse;\"\">\r\n<div style=\"\"width: 0; height: 0; border-left: 18px solid transparent; border-right: 18px solid transparent; border-bottom: 33px solid black; margin: 5px 5px 5px 5px;\"\" >\r\n<p style=\"\"font-family:Arial; color:#ff7700; \r\nfont-size:32px; margin:5px -5px 0px -5px;\"\">!</p>\r\n<p style=\"\"color:black; font-size:32px; margin:-45px 30px; font-family:'Arial Black', Arial; font-weight:'900';\"\">WARNING</p>\r\n</div>\r\n</td>\r\n</tr>\r\n<tr>\r\n<td style=\"\"border: 5px solid black; border-collapse:collapse;\"\">\r\n<p style=\"\"font-size:12px; font-family:'Arial Black', Arial; font-weight:'900'\"\">You are about to perform a test that\r\ninvolves pressurized gasoline being\r\npumped. Before performing this\r\ntest, be sure the area is free and\r\nclear from combustibles, open\r\nflame, welding and/or grinding\r\nsparks, or anything else that could\r\nignite or be an ignition source.<br>\r\n<br>\r\nFAILURE TO FOLLOW THIS WARNING\r\nMAY CAUSE INADVERTENT IGNITION\r\nTHAT COULD RESULT IN SERIOUS\r\nINJURY OR DEATH.</p>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>\""

Ignore the fact that the last two columns are being filled with 2s and 3s, that's just a place holder until I can figure out how to do this because they will also be filled using an array of strings. Is there a special character in the string element that it is getting an error on? It makes it through about 10 other elements which look similar to this one, but gets an error on this one.

I've exhausted every option I can think of so any help would be greatly appreciated.

Statement has Single Quote for

font-family:'Arial Black', Arial; 

You should try this. Replace Single Quote with Double Single Quote.

for (int i = 0; i <= 20; i++)
{
     command.CommandText = string.Format("INSERT INTO [Table] (Id, Tag, Description) VALUES ('{0}','2','3')", diff1[i].Replace("'","''"));
     command.ExecuteNonQuery();
}

try below

command.CommandText ="INSERT INTO [Table] (Id, Tag, Description) VALUES (@id,'2','3')";

command.Parameters.Add(new SqlParameter("@id", ""));

for (int i = 0; i <= 20; i++)
{
    command.Parameters["@id"].Value = diff1[i];
    command.ExecuteNonQuery();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM