I am using C# to connect to an access database and checking if a table exists in that database. This is the syntax I am using, but for a few databases that have lots of tables in them it takes quite some time to execute. Is their a faster way to do this as speed is the most important thing for this procedure?
string[] tableNames = new string[4] { "One", "Two", "Three", "Four" };
for (int q = tableNames.GetLowerBound(0); q <= tableNames.GetUpperBound(0); q++)
{
foreach (DAO.TableDef tabledef in dd.TableDefs)
{
string strtable = tableNames[q];
if (tabledef.Name == strtable) { found = true; }
if (found) { dd.TableDefs.Delete(strtable); }
}
}
For any future travelers who might stumble upon this, this was my final syntax that I used --- Exponentially faster!!!
Last EDIT --- I changed the Execute
statement to be encapsulated in a try/catch
block as if the table name that is listed in the array does not actually exist it will throw an error.
System.Data.OleDb.OleDbConnection oleconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + PathToDatabase" + ".mdb;");
oleconn.Open();
string[] tableNames = new string[4] { "One", "Two", "Three", "Four" };
for (int q = tableNames.GetLowerBound(0); q <= tableNames.GetUpperBound(0); q++)
{
System.Data.OleDb.OleDbCommand cmd = new OleDbCommand("DROP TABLE " + tableNames[q], oleconn);
try { cmd.ExecuteNonQuery(); }
catch {}
}
oleconn.Close();