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
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