I was trying to insert data into the second sheet in an excel file, but it gives syntax error. I tried inserting with the same syntax in sheet1 and it was working fine. The sheet name is correct, column names are correct. My code is
try
{
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='d:\\contacts_moorthy.xls';Extended Properties=Excel 8.0;");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "insert into [Sheet2$] (To,Message,SentOn) values ('" + phonetxtbox.Text + "','" + messagetxtbox.Text + "','"+DateTime.Now+"')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MyConnection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error :",ex);
}