Deleting MDB after creating it, causes "file is in use by another process"

K

kris.dorey

Hi,

Ive got the following code which seems ok but when the user runs the
function for a second time I get an error message stating that the mdb
is in use by another process. There is still an ldb for the life of the
application even after calling oldebconnection.close and gc.collect.

Any ideas?


[Code Starts]
private void btnRecreateDatabase_Click(object sender,
System.EventArgs e)
{
string mdbFileName = Application.StartupPath + @"\Eve21.mdb";
// Delete mdb file if already exists
if (System.IO.File.Exists(mdbFileName))
{
MessageBox.Show("Deleting MDB");
System.IO.File.Delete(mdbFileName);
}

Type objClassType = Type.GetTypeFromProgID("ADOX.Catalog");

if (objClassType != null)
{
object obj = Activator.CreateInstance(objClassType);
// Create mdb file
obj.GetType().InvokeMember("Create",
System.Reflection.BindingFlags.InvokeMethod, null, obj, new
object[]{
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbFileName + ";"
});

if (System.IO.File.Exists(mdbFileName))
{
using (OleDbConnection connection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
mdbFileName
+ ";Persist Security Info=False"))
{
connection.Open();
if (connection.State == System.Data.ConnectionState.Open)
{
MessageBox.Show("Connection closing");
connection.Close();
GC.Collect();
}
}
}
}




}
[Code Ends]

Error appears in the
Type objClassType = Type.GetTypeFromProgID("ADOX.Catalog");

line but I think this is misleading and its being caused by the
file.delete.


Thanks in Advance
 
W

Wessel Troost

is in use by another process. There is still an ldb for the life of the
application even after calling oldebconnection.close and gc.collect.
It helps if you explicitly free all your COM objects with a call to
System.Runtime.InteropServices.Marshal.ReleaseComObject.

Greetings,
Wessel
 
P

Peter Bromberg [C# MVP]

Ok, well you can call GC.Collect until the cows come home, but the fact of
the matter is when you are dealing with a file-based database engine such as
Jet (Access) you are going to have to take extreme care to release all COM
object references and even then, you may still find that IIS has tied up the
file in some way. Suggest you look at alternatives such as MSDE or
preferably, SQL Express (SQL Server 2005) both of which are currently free.
--Peter
 
V

Vagabond Software

Hi,

Ive got the following code which seems ok but when the user runs the
function for a second time I get an error message stating that the mdb
is in use by another process. There is still an ldb for the life of the
application even after calling oldebconnection.close and gc.collect.

Any ideas?


[Code Starts]
private void btnRecreateDatabase_Click(object sender,
System.EventArgs e)
{
string mdbFileName = Application.StartupPath + @"\Eve21.mdb";
// Delete mdb file if already exists
if (System.IO.File.Exists(mdbFileName))
{
MessageBox.Show("Deleting MDB");
System.IO.File.Delete(mdbFileName);
}

Type objClassType = Type.GetTypeFromProgID("ADOX.Catalog");

if (objClassType != null)
{
object obj = Activator.CreateInstance(objClassType);
// Create mdb file
obj.GetType().InvokeMember("Create",
System.Reflection.BindingFlags.InvokeMethod, null, obj, new
object[]{
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + mdbFileName + ";"
});

if (System.IO.File.Exists(mdbFileName))
{
using (OleDbConnection connection = new
OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
mdbFileName
+ ";Persist Security Info=False"))
{
connection.Open();
if (connection.State == System.Data.ConnectionState.Open)
{
MessageBox.Show("Connection closing");
connection.Close();
GC.Collect();
}
}
}
}




}
[Code Ends]

Error appears in the
Type objClassType = Type.GetTypeFromProgID("ADOX.Catalog");

line but I think this is misleading and its being caused by the
file.delete.


Thanks in Advance

Perhaps this is a very stupid suggestion, but do you happen to have an
Anti-Virus program? I've had problems creating or copying files and then
trying to delete them because the company anti-virus' "paranoia" settings
are scanning files I'm trying to delete.

Again, this is probably not the problem, but it is something to be aware or.

Good Luck,

Carl
 
Top