Unknown error reading Excel file with blank rows using WHERE clause

excel oledbcommand oledbdatareader sqlbulkcopy

Question

So clients upload Excel files to us and we have a windows service that periodically grabs new files and bulk-loads the contents to our SQL server like so:

string excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=YES;\"";

using (OleDbConnection excelConnection = new OleDbConnection(excelConnectionString))
{
  excelConnection.Open();
  OleDbCommand cmd;
  cmd = new OleDbCommand("Select " + fileID.ToString() + " as [FileID],[AccountName],[Author],[Title],[Body] from [Sheet1$] where [Body] is not null;", excelConnection);
  OleDbDataReader dReader;
  dReader = cmd.ExecuteReader();
  using (SqlBulkCopy sqlBulk = new SqlBulkCopy(ConfigurationManager.ConnectionStrings["VI.OpenAmplify.Properties.Settings.DBConnection"].ConnectionString))
  {
      sqlBulk.DestinationTableName = "Web_OpenAmp_Posts";
      sqlBulk.ColumnMappings.Add("FileID", "FileID");
      sqlBulk.ColumnMappings.Add("AccountName", "AccountName");
      sqlBulk.ColumnMappings.Add("Author", "Author");
      sqlBulk.ColumnMappings.Add("Title", "Title");
      sqlBulk.ColumnMappings.Add("Body", "Body");

      sqlBulk.WriteToServer(dReader);
  }
}

Works great most of the time, even if there are some blank rows at the bottom (the WHERE clause takes care of that). But sometimes we get these weird files with a lot (about a million) blank rows at the bottom and we get an Exception with Unknown as the message. If I remove the WHERE clause from OleDbCommand declaration, the file loads fine, but we get a million blank rows inserted as well. Any ideas on how to deal with this?

Accepted Answer

We had Microsoft Access Database Engine 2010 Redistributable drivers x_64 flavor installed on our 64-bit windows server. When compiling project to be run with such configuration, Platform Target for the project needs to be set to Any CPU. Somehow we had x86 selected by default (which works on dev machine). Changing this setting fixed the issue.



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why