public void RefetchDatabaseCache()
{
using (DbConnection conn = GetNewConnection())
using (ExSchemaEngine engine = GetSchemaEngine(conn))
{
StringCollection dbTables, dbViews;
// Open the connection
conn.Open();
// Woot! no error!
// the schema engine
// shcema engine options
engine.SpecificOwner = this.SqlUsername;
// Read schema list from db
engine.ReadViewsTablesList(out dbTables, out dbViews);
List<SelectedTablesType> newTables = new List<SelectedTablesType>();
List<SelectedTablesType> newViews = new List<SelectedTablesType>();
// Adding new tables
foreach (var tableName in dbTables)
{
bool added = false;
// looking for previous setting
foreach (var prevTable in Tables)
if (prevTable.Name == tableName)
{
newTables.Add(new SelectedTablesType()
{
Name = tableName,
Selected = prevTable.Selected
});
added = true;
break;
}
if (added) continue;
// nothing found, add as not selected
newTables.Add(new SelectedTablesType()
{
Name = tableName,
Selected = false
});
}
// Adding new views
foreach (var viewName in dbViews)
{
bool added = false;
// looking for previous settings
foreach (var prevView in Views)
if (prevView.Name == viewName)
{
newViews.Add(new SelectedTablesType()
{
Name = viewName,
Selected = prevView.Selected
});
added = true;
break;
}
if (added) continue;
// nothing found, add as not selected
newViews.Add(new SelectedTablesType()
{
Name = viewName,
Selected = false
});
}
// Replacing the old ones
Tables.Clear();
Views.Clear();
Tables = newTables;
Views = newViews;
// sort
Views.Sort((x, y) => string.Compare(x.Name, y.Name));
Tables.Sort((x, y) => string.Compare(x.Name, y.Name));
}
}