Closing Access file created with ADOX.CatalogClass

  • Thread starter Thread starter VMI
  • Start date Start date
V

VMI

I'm creating an Access file with the following code, but won't let me erase
it after creating it. How can I close the file immediately after creating
it?

ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + sFileName + ";" +
"Jet OLEDB:Engine Type=5");
cat=null; // Does NOT work



Thanks.
 
Hi,

Try this:

Marshal.ReleaseComObject(cat);
cat = null;
GC.Collect(); // This is the last resort - don't use if ReleaseComObject
alone helps.
 
It worked. Thanks.


Dmitriy Lapshin said:
Hi,

Try this:

Marshal.ReleaseComObject(cat);
cat = null;
GC.Collect(); // This is the last resort - don't use if ReleaseComObject
alone helps.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

VMI said:
I'm creating an Access file with the following code, but won't let me
erase it after creating it. How can I close the file immediately after
creating it?

ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + sFileName + ";" +
"Jet OLEDB:Engine Type=5");
cat=null; // Does NOT work



Thanks.
 
Release and unlock an Access database file (.mdb) created using ADOX

VMI said:
I'm creating an Access file with the following code, but won't let me erase
it after creating it. How can I close the file immediately after creating
it?
When you create the database, a connection is created to the database automatically. You must close the connection to release the lock on the file. The connection is in the catalog's ActiveConnection property.

Using COM references:
1) Microsoft ActiveX Data Objects 2.8 Library
2) Microsoft ADO Ext. 2.8 for DDL and Security

Sample code:

private void CreateDatabaseFile(string filename)
{
ADOX.CatalogClass catalog = new ADOX.CatalogClass();
catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename + ";Jet OLEDB:Engine Type=5");

ADODB.Connection connection = catalog.ActiveConnection as ADODB.Connection;
if (connection != null)
{
connection.Close();
}
catalog.ActiveConnection = null;
catalog = null;
}
 
Last edited:
I have the some problem but this solution doesn't work for me. I want to destroy database that i create but i can't. I get this exception:

Additional information: The process cannot access the file "C:/.../vIP." because it is being used by another process.

The code that I use is:

*****************************************************
ADOX.CatalogClass catalog = new ADOX.CatalogClass();


catalog.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +

"Data Source=C:/.../vIP.;" + "Jet OLEDB:Engine Type=5");


System.Runtime.InteropServices.Marshal.ReleaseComObject(catalog);

catalog =
null;

GC.Collect();



System.IO.File.Delete("C:/.../vIP.");

******************************************************

I now that this problem cause ADOX.CatalogClass catalog because without it, it works, but in that case I create database manual and this is not good choice for me.

Please help. Thanks.
 
haggy did you got the solution to destroy database

hi,

did you got the solution to destroy database.

if so, please reply!

Thanks
 
I'm creating an Access file with the following code, but won't let me erase
it after creating it. How can I close the file immediately after creating
it?

ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + sFileName + ";" +
"Jet OLEDB:Engine Type=5");
cat=null; // Does NOT work



Thanks.

I recently experienced this problem.If your development machine is 32 bit while production machine is 64 bit then following code may just save your day!
if (IntPtr.Size == 8)//if the os is 64 bit
{
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source=E:\\new folder\\yourfilename.mdb;" +
"Jet OLEDB:Engine Type=5");
cat = null;
}
else
{
if (IntPtr.Size == 4)//if the os is 32 bit
{
ADOX.CatalogClass cat = new ADOX.CatalogClass();
cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=E:\\new folder\\yourfilename.mdb;" +
"Jet OLEDB:Engine Type=5");
cat = null;
}
 
Back
Top