繁体   English   中英

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

[英]OleDbCommand use brackets '[' ']' in a query as table name

我有一个Excel文件,其中包含以下列名称: nameDisplay 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],[Display name [de-DE]],[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..

尝试这样的事情

Insert into [foo$] ('[Name]','[Display name [de-DE]]','[Comment [de-DE]]',

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM