查询表达式中的语法错误(缺少运算符),Oledb INSERT语句

时间:2013-06-07 18:46:18

标签: c# oledb export-to-excel

我知道已经发布了很多这些帖子,但我查看了很多这些帖子并且它们看起来都特定于一个人的需求,所以我想我是否会有人能帮助我满足我的特定需求。

我正在使用Oledb和c#,我正在尝试创建一个Excel工作表并填充它但是在使用此INSERT语句时我收到错误“查询表达式中的语法错误(缺少运算符)”:

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

为了解释,我有一个字符串数组diff1 [],我正在使用循环将每个元素插入到它自己的单元格中,它一直有效,直到它到达字符串元素:

"\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>\""

忽略最后两列用2和3填充的事实,这只是一个占位符,直到我能弄明白如何做到这一点,因为它们也将使用字符串数组填充。字符串元素中是否有一个特殊字符,它会出错?它通过大约10个看起来与此类似的其他元素,但在这个上面会出错。

我已经筋疲力尽了我能想到的每一个选择,所以任何帮助都会受到高度赞赏。

2 个答案:

答案 0 :(得分:3)

声明有单引号

font-family:'Arial Black', Arial; 

你应该试试这个。用双单引号替换单引号。

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

答案 1 :(得分:1)

尝试以下

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