如果列标题中存在空格...AND/OR 运算符在 C# .NET 中的 sql 语句中不起作用,无法修改 Excel 工
本文关键字:语句 不起作用 sql 中的 Excel 修改 NET 空格 存在 标题 AND | 更新日期: 2023-09-27 18:36:06
如果我在sql语句中使用and/or语句,则会发生运行时错误。请帮我解决问题
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand;
myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MessageBox.Show("Opening " + selectedPath);
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + rootPath+"FacultyDutiesEnd.xls" + "';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "Update [Sheet1$] set A1 = ''u221A',A2 = ''u221A',B1 = ''u221A',B2 = ''u221A',C1 = ''u221A',C2 = ''u221A',D1 = ''u221A',D2 = ''u221A',E1 = ''u221A',E2 = ''u221A',F1 = ''u221A',F2 = ''u221A',G1 = ''u221A',G2 = ''u221A'";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
sql = "Update [Sheet1$] set A1 = 'a' where Designation = 'Prof(senior)' and (Faculty Name = 'bob') ";// this line is not being executed unless the and (Faculty Name = 'bob') is removed
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
错误太长,,开头为System.data.oledb.oledbException(0x80040E14):查询表达式"指定 = 教授"和"名称 = 鲍勃"中的语法错误(缺少运算符)。
如果
和学院名称 ='bob' 替换为
并且 A2='''u221A' 被使用它正在工作
更新
如果我在设计之间保留空格...即使它不起作用...那么有没有办法让列标题有空格并执行sql语句呢?
我很
确定问题出在您的Faculty Name
列名称中的空格,这与使用 AND
或 OR
无关。请尝试:
sql = "Update [Sheet1$] set A1 = 'a' " +
"where Designation = 'Prof(senior)' and [Faculty Name] = 'bob'";