Using the WHERE clause to read an Excel file with blank rows causes an unknown error.

excel oledbcommand oledbdatareader sqlbulkcopy


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))
  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");


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?

10/25/2011 1:36:00 AM

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.

3/1/2012 4:57:13 PM

Related Questions

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow