I have an Integration Services package (VS 2012) that loads values from an Excel workbook and stores them in SQL Server. I'm using a 3rd party library to extract the workbook data in a C# script task, but before that, in the task, I create a Filestream:
MessageBox.Show("Step 1");
using (FileStream fsWorkbook = new FileStream(strWkbkFilePath, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite))
{ MessageBox.Show("Step 2");
//...
}
The FileStream construction crashes with the error "The process cannot access the file ... because it is being used by another process." I know that this is where the crash occurs, because I see the "Step 1" message box before the exception
message, but not "Step 2".
What's frustrating is that, if I set a breakpoint in the the script task, before the line causing the error, and start stepping through the code, I'm able to proceed and access the workbook file without error. At first I thought this might be due to a race
condition, but then I added the message box shown above, which also pauses execution before the file stream is created. The pausing due to the MessageBox doesn't prevent the error when running in non-debug mode.
Note that I'm using FileShare.ReadWrite in the FileStream constructor, even though no other process should have the workbook open at the time.
So why do I get the sharing error? And why does it not happen when debugging?