Saher Isaac
Saher Isaac

Reputation: 71

Insert data to Excel table using OLEDb

My code looks like

    using System.Data.OleDb;


    namespace ConsoleApplication21
    {
        class Program
        {
            static void Main(string[] args)
            {
                string constr2 = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\productivity\temp.xlsx;Extended Properties='Excel 8.0;HDR=no'";

            OleDbConnection conn = new OleDbConnection(constr2);
            conn.Open();

            OleDbCommand INSERTcommand = new OleDbCommand();
            INSERTcommand.Connection = conn;
            INSERTcommand.CommandText = "create table [table3] (id INT, name VARCHAR, datecol DATE);";
            INSERTcommand.ExecuteNonQuery();

            OleDbCommand INSERTDATA = new OleDbCommand();
            INSERTDATA.Connection = conn;
            INSERTDATA.CommandText = "INSERT INTO [table3] (id,name,datecol) values (1,'aaaa','25-01-2012');";
            INSERTDATA.ExecuteNonQuery();

        }
    }
}

After executing the above code an exception shows:

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

Upvotes: 0

Views: 1151

Answers (1)

Falco Alexander
Falco Alexander

Reputation: 3332

if you omit the column specifications while inserting, it works fine:

INSERTDATA.CommandText = "INSERT INTO [table3] values (1,'aaaa','25-01-2012');";

Upvotes: 1

Related Questions