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

时间:2014-05-25 12:14:17

标签: c# oledb oledbcommand

我是c#的初学者,我试过通过很多帖子尝试解决我的问题,但我没有运气,所以我想我也可以创建一个帖子来寻求帮助。基本上,我正在尝试使用一些额外的信息更新excel文件,但问题是我每次运行源代码时都会遇到此错误:

System.Data.dll中出现未处理的“System.Data.OleDb.OleDbException”类型异常

附加信息:查询表达式“无线立体声耳机(TAK)(已损坏)”中的语法错误(缺少运算符)。

我已经尝试了很多方法来解决它,包括使用INSERT语句而不是UPDATE语句,但似乎没有任何工作。

再次感谢帮助=)。

int m_intRecipientESMID = Convert.ToInt32(m_recipientESMID);

//Converting Row integer to string
string m_excelRowCoordinateLoanItemTrackerStr = m_excelRowCoordinateLoanItemTracker.ToString();

//Creating a connection directory to access the Excel "LoanItemTracker" file
string m_pathSourceExcelLoanItemTrackerNOHDR = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + textBoxExcelLoanItemTrackerLocation.Text + @";Extended Properties=""Excel 8.0; HDR=NO;ImportMixedTypes=Text;TypeGuessRows=0""";
OleDbConnection m_pathConnectionExcelLoanItemTrackerNOHDR = new OleDbConnection(m_pathSourceExcelLoanItemTrackerNOHDR);

m_pathConnectionExcelLoanItemTrackerNOHDR.Open();   //Open Excel File LoanItemTracker.xlsx to conduct Write/Update process

string m_scannedItemName2 = "Wireless Stereo Headphone (TAK)      (damaged)";

// write the loaned item name into the Excel Spread sheet "LoanItemTracker.xls"
string m_excelRowColumnLoanItemNameCoord = String.Concat(m_excelColumnCoordinateLoanItemNameLoanItemTracker, m_excelRowCoordinateLoanItemTrackerStr);
string m_LoanItemNameWriteCommand = String.Format("UPDATE [Sheet1${0}:{0}] SET F1={1}", m_excelRowColumnLoanItemNameCoord, m_scannedItemName2);
OleDbCommand m_LoanItemTrackerCommandWrite = new OleDbCommand(m_LoanItemNameWriteCommand, m_pathConnectionExcelLoanItemTrackerNOHDR);

m_LoanItemTrackerCommandWrite.ExecuteNonQuery();

m_pathConnectionExcelLoanItemTrackerNOHDR.Close();

2 个答案:

答案 0 :(得分:1)

你必须在文字旁边加上引号:

String.Format("UPDATE [Sheet1${0}:{0}] SET F1='{1}'", m_excelRowColumnLoanItemNameCoord, m_scannedItemName2);

我把它们放在{1}之前和之后。此外,最好使用参数化查询。你也不需要引号。

答案 1 :(得分:0)

检查一下:UPDATE [Sheet1${0}:{0}] SET F1='{1}'也许你忘记了引号。