Recently this community helped me to get a list of the tables from an MXD file:
IMapDocument mapDoc = new MapDocument();
mapDoc.Open("C://mymxd.mxd");
IActiveView act = mapDoc.ActiveView;
IMap map = act.FocusMap;
IStandaloneTableCollection coleccionTablas = (IStandaloneTableCollection)map;
for (int i = 0; i < coleccionTablas.StandaloneTableCount; i++)
{
IStandaloneTable stTable = coleccionTablas.get_StandaloneTable(i);
string nameTOC = stTable.Name;
}
That way I get the name of the table as shown in the "Table of Contents" of ArcMap. I thought that from there, it would be easy to get the name of the table as stored in the database. In other words: I need the datasource of the table.
I found this code: Changing data source of table using ArcObjects?
That same solution is the only one I have found so far in different sites. It seems pretty easy, but I only get ITable
objects that are null... so getting the datasource for those is impossible.
This is my code:
for (int i = 0; i < coleccionTablas.StandaloneTableCount; i++)
{
IStandaloneTable stTable = coleccionTablas.get_StandaloneTable(i);
string nameTOC = stTable.Name;
ITable table = stTable.Table; //--> table is always null!!
IDataset dataset = (IDataset)table;
IDatasetName datasetName = (IDatasetName)dataset.FullName;
}
I have tested several MXD files with tables, but the ITable
object I get from the IStandaloneTable
is always null and I have no idea why.
I have also tried with ITableCollection
, but the result is the same.
Does anyone know why I keep getting nulls? Maybe there is something horribly wrong with my code.
nameTOC
is that actually setting to the name of the table in TOC? Also your first bit of code is accessing a MapDocument object, are you actually wanting to get the tables in the currently opened document, in that case you want to be accessing the map from mxdocument.stTable
toIDataLayer2
then callingIDataLayer2.Connect
? (Maybe for performance MapDocument does lazy loading of standalone tables since MapControl can't display them anyway).datasetName
toIName
and then callIName.Open()
, which should return an object that you can cast toITable
.