Syntax error in INSERT INTO statement./Excel/oledb

309 Views Asked by At

I am trying to update an MS-Excel file using OledbDataAdapter and I am getting "syntax error in Insert Into statement"

This is my code:

  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

There are 1 best solutions below

0
On

I have decided to give ClosedXML a try: http://closedxml.codeplex.com/