locked
table refresh problem RRS feed

  • Question

  • For me refresh method on Table object does not work correctly as it does not refresh indexes collection. Consider the following code:
    //first try to find out what is the name of PK, note that table.Indexes collection is used
    string pkname = null;
    foreach (Index index in table.Indexes)
    {
      if (index.IndexKeyType == IndexKeyType.DriPrimaryKey)
        pkname = index.Name;
    }
    //next try to drop it (btw, if someone knows better way to delete primary index I'd be glad to hear anout it)
    try
    {
      if (pkname != null)
      {
        Console.WriteLine("drop pk: " + pkname);
        db.ExecuteNonQuery("ALTER TABLE [" + table.Schema + "].[" + table.Name + "] DROP CONSTRAINT [" + pkname + "]");
       }
    }
    catch (Exception e)
    {
      Console.WriteLine(e.Message);
    }
    //try to refresh database and table
    db.Refresh();
    table.Refresh();
    //now it turns out that deleted index is still in collection
    foreach (Index index in table.Indexes)
    {
      Console.WriteLine("index: " + index.Name);
    }

    It all seems like too much caching. If I hadn't used table.Indexes collection before executing "ALTER TABLE..." then all would work just fine. So the final question: how do I properly refresh table properties?
    Wednesday, June 6, 2007 11:00 AM

Answers

  • Since table.Refresh() refreshes only table properties , not collections, try : table.Indexes.Refresh(). This should help.
    Wednesday, June 6, 2007 11:09 AM

All replies

  • Since table.Refresh() refreshes only table properties , not collections, try : table.Indexes.Refresh(). This should help.
    Wednesday, June 6, 2007 11:09 AM
  • So obvious. I somehow subconsciously assumed that TableCollection cannot know anything about sql server so that refresh is not possible. Thanks a lot.
    Wednesday, June 6, 2007 11:32 AM