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.
1 Answer 1
Thanks to your comments, I've been able to get the datasource of my tables.
This morning I have realized my code had been tested against tables with a broken datasource. I have created an MXD with a table without a broken datasource and the code I posted in my question works without any problem.
But I also need to get the datasource of a table if it's broken. As suggested, I've tried to cast it to IDataLayer2
, but I haven't been able to get the datasource (I guess I'm doing something wrong).
Instead of casting it to IDataLayer2
, I have casted it to IDataLayer
.
IStandaloneTable stTable = coleccionTablas.get_StandaloneTable(i);
var dataLayer = (IDataLayer)stTable;
var datasetName = (IDatasetName)dataLayer.DataSourceName;
string name = datasetName.Name;
That works for broken and unbroken datasources.
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
.