The INSERT INTO statement contains the following unknown field name: 'a'. Make sure you have typed the name correctly, and try the operation again

旧巷老猫 提交于 2020-01-03 03:13:11

问题


// e,g, I want to insert 3 columns in 1st row and 10 columns in rest of the rows //I am Creating excel file with sheet name as MySheet // then updating value in 1st row, 1st cell of header as blank //then inserting data // Can anyone please help to insert data in excel without header

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName +
    ";Mode=ReadWrite;Extended Properties=\"Excel 12.0 XML;HDR=NO\"";

    using (OleDbConnection conn = new OleDbConnection(connectionString))
    {
    conn.Open();

    using (OleDbCommand cmd = new OleDbCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = "CREATE TABLE [MySheet] (a string)";
        cmd.ExecuteNonQuery();

        cmd.CommandText = "UPDATE [MySheet$] SET F1 = \"\"";
        cmd.ExecuteNonQuery();

        cmd.CommandText = "INSERT INTO  [MySheet] (a) values ('ABC')" //<-----Getting error to insert
        cmd.ExecuteNonQuery();
    }
}
    conn.Close();

回答1:


Just try [removed the (a)]

cmd.CommandText = "INSERT INTO  [MySheet] values ('ABC')"; 



回答2:


I get the same error. There was a padded space ending column name in Excel. Removing that space from end of column name fixed the issue.



来源:https://stackoverflow.com/questions/16481096/the-insert-into-statement-contains-the-following-unknown-field-name-a-make-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!