我正在尝试使用OledbDataAdapter更新MS-Excel文件,我收到“Insert Into语句中的语法错误”
这是我的代码:
private void btnLoadPremiumDetail_Click(object sender, RoutedEventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog();
string fileName = "";
if (openFileDialog.ShowDialog() == true)
fileName = openFileDialog.FileName;
else
return;
string sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileName + ";Extended Properties='Excel 12.0;HDR=No;IMEX=1'";
this.connection = new OleDbConnection(sConnection);
this.connection.Open();
var dtTablesList = connection.GetSchema("Tables");
var sSheetName = "";
if (dtTablesList.Rows.Count > 0)
sSheetName = dtTablesList.Rows[0]["TABLE_NAME"].ToString();
dtTablesList.Clear();
dtTablesList.Dispose();
if (!string.IsNullOrEmpty(sSheetName)) {
var command = new OleDbCommand("Select * From [" + sSheetName + "]", connection);
this.adapter = new OleDbDataAdapter(command);
new OleDbCommandBuilder(this.adapter);
this.table = new DataTable();
this.adapter.Fill(table);
this.dgExcelData.ItemsSource = table.DefaultView;
}
}
private void btnSave_Click(object sender, RoutedEventArgs e) {
this.adapter.Update(this.table);
this.lblStatus.Content = "Data saved";
}