我试图将数据插入到 Excel 文件的第二张工作表中,但它给出了语法错误。我尝试在 sheet1 中使用相同的语法插入,它工作正常。工作表名称正确,列名称正确。我的代码是
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);
}