Reputation: 388
This is the C# code I use to export the queries from an ms-access-database:
var appClass = new ApplicationClass();
appClass.Visible = false;
appClass.UserControl = false;
appClass.OpenCurrentDatabase(@"C:\RadLokal\trunk\Test\Access\Test.accdb", false, "");
var db = appClass.CurrentDb();
var queryDefs = db.QueryDefs;
for (int i = 0; i < queryDefs.Count; i++)
{
var item = queryDefs[i];
const string QueryPrefix = "Query_";
var filename = Path.Combine(@"c:\Temp", QueryPrefix + item.Name + ".txt");
File.WriteAllText(filename, item.SQL);
item.Close();
}
db.Close();
appClass.CloseCurrentDatabase();
appClass.Quit(AcQuitOptionWrapper.acQuitSaveNone);
The database is opened and all the queries are exported. But unfortionally a empty ms-access instance stays open. It is not possible to close this instance by hand (via the x). It can only get closed with the task manager. It is closed automaticly when I close the host process.
Things I tried to solve my problem:
Upvotes: 0
Views: 107
Reputation: 388
I had to set all my variables to null and call GC.Collect afterwards. This is the code that worked for me:
var appClass = new ApplicationClass();
appClass.Visible = false;
appClass.UserControl = false;
appClass.OpenCurrentDatabase(@"C:\RadLokal\trunk\Test\Access\Test.accdb", false, "");
var db = appClass.CurrentDb();
var queryDefs = db.QueryDefs;
for (int i = 0; i < queryDefs.Count; i++)
{
var item = queryDefs[i];
const string QueryPrefix = "Query_";
var filename = Path.Combine(@"c:\Temp", QueryPrefix + item.Name + ".txt");
File.WriteAllText(filename, item.SQL);
item.Close();
item = null;
}
queryDefs = null;
db.Close();
db = null;
appClass.CloseCurrentDatabase();
appClass.Quit(AcQuitOption.acQuitSaveNone);
appClass = null;
GC.Collect();
Upvotes: 0