0

I have a Excel file which contains the following column names: name,Display name [de-DE],Comment [de-DE].

enter image description here

If I use this query, Insert into [foo$] ([Name],[Display name [de-DE]],[Comment [de-DE]], I get an exception:.

Syntax error in INSERT INTO statement

How do I have to escape the brackets to use them in the query?

sample code

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();

workaround

The only working solution so far is based on this answer: https://stackoverflow.com/a/12499797/6229375

One option is to remove the column declaration completely and just do insert into..

Dominic Jonas
  • 4,717
  • 1
  • 34
  • 77

1 Answers1

0

Try something like this

Insert into [foo$] ('[Name]','[Display name [de-DE]]','[Comment [de-DE]]',
Mukul Keshari
  • 495
  • 2
  • 7