INSERT INTO语句中的语法错误./Excel/oledb

时间:2014-12-15 11:25:11

标签: c# excel oledbdataadapter

我正在尝试使用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";
        }

1 个答案:

答案 0 :(得分:0)

我决定尝试使用ClosedXML:http://closedxml.codeplex.com/