OleDbCommand在查询中使用方括号'['']'作为表名

我有一个Excel文件,其中包含以下列名称:nameDisplay name [de-DE]Comment [de-DE]

OleDbCommand在查询中使用方括号'['']'作为表名

如果我使用此查询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..

eyro77 回答:OleDbCommand在查询中使用方括号'['']'作为表名

尝试这样的事情

Insert into [foo$] ('[Name]','[Display name [de-DE]]','[Comment [de-DE]]',
本文链接:https://www.f2er.com/3096495.html

大家都在问