G
Guest
I have a function that imports an Excel file into an Access table using SQL.
I then close the OleDataReader and the OleDbConnection, then dispose the
OleDbCommand, then OleDbConnection. The calling function then attempts a
File.Delete (also tried the FileInfo delete) on the Excel file, but gets the
message "The process cannot access the file ... because it is being used by
another process".
I've stepped through the code and it steps through correctly. I've put the
delete before the import function call and it works correctly. But it doesn't
work if it follows the import call.
Here's the important part of the import function code:
OleDbConnection cnSQL;
OleDbCommand cmSQL;
....
cnSQL = new OleDbConnection(ConnectionString);
cnSQL.Open();
strSQL = "INSERT INTO " + FileType + " SELECT * FROM [Excel 8.0;Database=" +
ExcelFile + "].[" + FileType + "Log]";
cmSQL = new OleDbCommand(strSQL, cnSQL);
cmSQL.ExecuteNonQuery();
cnSQL.Close();
cmSQL.Dispose();
cnSQL.Dispose();
I then close the OleDataReader and the OleDbConnection, then dispose the
OleDbCommand, then OleDbConnection. The calling function then attempts a
File.Delete (also tried the FileInfo delete) on the Excel file, but gets the
message "The process cannot access the file ... because it is being used by
another process".
I've stepped through the code and it steps through correctly. I've put the
delete before the import function call and it works correctly. But it doesn't
work if it follows the import call.
Here's the important part of the import function code:
OleDbConnection cnSQL;
OleDbCommand cmSQL;
....
cnSQL = new OleDbConnection(ConnectionString);
cnSQL.Open();
strSQL = "INSERT INTO " + FileType + " SELECT * FROM [Excel 8.0;Database=" +
ExcelFile + "].[" + FileType + "Log]";
cmSQL = new OleDbCommand(strSQL, cnSQL);
cmSQL.ExecuteNonQuery();
cnSQL.Close();
cmSQL.Dispose();
cnSQL.Dispose();