Excel ファイルの 2 番目のシートにデータを挿入しようとしましたが、構文エラーが発生します。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);
}