I have a legacy code that imports Excel (*.xls) to our db, then move the file to specific directory after processing.
The code works fine except in one case, when the file is corrupted (even MS Excel cannot open it)! What happens in this case is that an System.AccessViolationException
thrown at opening the connection!
Here is how the code looks like:
string connectionString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""", filePath);
OleDbConnection connection = new OleDbConnection(connectionString);
try
{
connection.ConnectionString = connectionString;
connection.Open(); //<<<--- exception throws here
//file processing
}
catch (Exception e)
{
//exception handling
}
finally
{
connection.Close();
connection.Dispose();
connection = null;
GC.Collect();
}
Here is the exception details...
System.AccessViolationException was caught
Message=Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Source=System.Data
StackTrace:
at System.Data.Common.UnsafeNativeMethods.IDBInitializeInitialize.Invoke(IntPtr pThis)
at System.Data.OleDb.DataSourceWrapper.InitializeAndCreateSession(OleDbConnectionString constr, SessionWrapper& sessionWrapper)
at System.Data.OleDb.OleDbConnectionInternal..ctor(OleDbConnectionString constr, OleDbConnection connection)
at System.Data.OleDb.OleDbConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningObject)
at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup)
at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
at System.Data.OleDb.OleDbConnection.Open()
As you can see, I am catching this exception and process it, then when the code try to move the file to another directory, I got the following exception:
System.IO.IOException occurred
Message=The process cannot access the file because it is being used by another process.
Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.File.Move(String sourceFileName, String destFileName)
I tried to use another library, like LinqToExcel, but found it internally uses the same implementation like mine, then it is has the same problem!
I tried also to run garbage collector after the connection is closed (as you see in the above code) but faced the same problem!
Any Idea?