我有一个Excel
文件,其中包含以下列名称:name
,Display name [de-DE]
,Comment [de-DE]
。
如果我使用此查询Insert into [foo$] ([Name],[Display name [de-DE]],[Comment [de-DE]]
,则会得到一个exception:
。
INSERT INTO语句中的语法错误
如何在查询中使用brackets
进行转义?
示例代码
var connectionString = $"Provider=microsoft.ACE.OLEDB.12.0;Data Source=\"{Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"foo.xlsx")}\";Extended Properties=\"Excel 12.0;HDR=YES;\"";
using (OleDbConnection conn = new OleDbConnection(connectionString))
{
conn.Open();
using (OleDbCommand cmd = new OleDbCommand())
{
try
{
cmd.Connection = conn;
cmd.CommandText = @"Insert into [Hmi Tags$] ([Name],[Comment [de-DE]])
VALUES ('foo1','foo2','foo3');";
cmd.ExecuteNonQuery();
Console.WriteLine("success");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Console.ReadKey();
解决方法
到目前为止,唯一可行的解决方案基于以下答案:https://stackoverflow.com/a/12499797/6229375
一种选择是完全删除列声明,然后执行
insert into..