How to insert data into excel using OleDbConnection

A

ar.gorgin

Guest
I have a excel file and defined two column Date,Result in it.

I want insert data in it with c#.

I use this code :

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory + "\\Data\\sms log.xlsx;Extended Properties=Excel 12.0;";

OleDbConnection myConnection = new OleDbConnection(connectionString);
if(myConnection.State==System.Data.ConnectionState.Closed) myConnection.Open();
OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
myCommand.Connection = myConnection;
myCommand.CommandText =string .Format( "Insert into TABLE [Sheet1$](DateTime,Result)values('{0}','{1}')",DateTime.Now,false) ;
myCommand.ExecuteNonQuery();
myConnection.Close();


But i get this error :Syntax error in INSERT INTO statement.

Continue reading...
 
Top