By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,154 Members | 1,056 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,154 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> 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.ReleaseComO bject.

Greetings,
Wessel
Nov 17 '05 #2

P: n/a
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

<kr********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 17 '05 #3

P: n/a
<kr********@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.