尝试插入Excel工作表时遇到错误
这是名为Sheet1的Excel工作表的布局:
id name
1 test
2 apple
3 pear
我想将此数据插入Excel工作表:
4 pineapple
这是我的代码:
try{
string srcExcel = "C:\\Users\\Desktop\\insertTest.xlsx";
System.Data.OleDb.OleDbConnection MyConnection;
System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
string sql = null;
MyConnection = new OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0; Data Source = " + srcExcel + "; Extended Properties = 'Excel 8.0;HDR=Yes;IMEX=1'");
MyConnection.Open();
myCommand.Connection = MyConnection;
sql = "Insert Into [Sheet1$] (id,name) Values('4','pineapple')";
myCommand.CommandText = sql;
myCommand.ExecuteNonQuery();
MessageBox.Show("Insert Success!");
MyConnection.Close();
}
catch (Exception eInnerData)
{
MessageBox.Show("Method: getInnerData " + eInnerData.ToString());
}
运行此代码后,将抛出异常并捕获此错误消息:
操作必须使用可更新的查询
我正在使用Microsoft Excel 2010