1

Cross post from: https://geonet.esri.com/thread/125010

Does anyone know how to change the data source of a table using arcobjects? I know it can be done with arcpy, but I'm using C#. I've managed to change the source of all the layers in the mxd, but I need to do tables too.


I've come up with a solution by adding and removing from the table collection.

Is this the best way to do it?

 IMap map = ...;
 IWorkspace = ...;
 ITableCollection tableCollection = (ITableCollection)map;
 int tableCount = tableCollection.TableCount;
 // iterate over a copy of the tables so we can add and remove
 List<ITable> tables = new List<ITable>();
 for (int i = 0; i < tableCount; i++)
 {
 tables.Add(tableCollection.get_Table(i));
 }
 foreach (ITable oldTable in tables)
 {
 IDataset dataset = (IDataset)oldTable;
 IDatasetName datasetName = (IDatasetName)dataset.FullName;
 string workspaceName = datasetName.WorkspaceName.PathName;
 // get the new table
 ITable newTable = ((IFeatureWorkspace)workspace).OpenTable(datasetName.Name);
 // replace the table in the TOC
 tableCollection.RemoveTable(oldTable);
 tableCollection.AddTable(newTable);
 // fire change event to change table
 IMapAdmin2 mapAdmin2 = (IMapAdmin2)map;
 mapAdmin2.FireChangeTable(oldTable, newTable);
 }

Note: this is simplified - I did check the table was in the new workspace before opening it.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Mar 13, 2015 at 11:48
2
  • 1
    Please explain what you mean by changing the data source of a table. Commented Mar 13, 2015 at 12:11
  • I've copied the table to another database and want to update all my mxds. I'm looking for something like this snippet but for tables instead of feature layers resources.arcgis.com/en/help/arcobjects-net/componenthelp/… Commented Mar 13, 2015 at 12:47

1 Answer 1

2

You should use the table property of IStandaloneTable to change its datasource. First create a reference to the new ITable (e.g. a table from a geodatabase), then use this code to replace the Table property:

 IMap map = ...;
 IStandaloneTableCollection tbcol= (IStandaloneTableCollection)map;
 for (int i = 0; i < tbcol.StandaloneTableCount; i++)
 {
 var stTable = tbcol.get_StandaloneTable(i);
 stTable.Table = newTable; //Here put the new ITable To change the datasource
 if (!stTable.Valid)
 MessageBox.Show("Table is not valid, "+stTable.Name);
 }
answered Mar 13, 2015 at 12:49
2
  • Thanks Farid - I came up with a solution using ITableCollection (see edit). Is there a reason for using IStandaloneTableCollection instead? Commented Mar 13, 2015 at 14:27
  • I don't see any differences here. But IStandaloneTableCollection is implemented with NALayer (esriNetworkAnalyst) class too. Meaning this is more complete that ITableCollection Commented Mar 13, 2015 at 14:43

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.