Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am using ArcObjects v10.0 with a SQL Server 2008 database. Given the name of a Layer, is there a way for me to extract the corresponding name of the database table that represents that Layer?

The Layer name and the Database Table name will often be identical, but they will not in cases where the Layer name contains invalid characters, such as spaces.

share|improve this question
add comment

2 Answers 2

Here you go:

IFeatureLayer fl = ...;
string tablename = ((IDataset)fl.FeatureClass).BrowseName;

Link: IDataset.BrowseName API Docs

share|improve this answer
    
Thanks, but how would you retrieve the IFeatureLayer, given only it's name? –  Seth P. Mar 28 '11 at 18:40
    
That would depend on what you're trying to get the layer from. Whether its a desktop tool working with an MXD, ArcObjects code working on server, etc. Generally though if you can get an IMap object, you can loop through all the layers by calling map.get_Layers() and use the layer name to find the right ILayer object, then cast that ILayer object to an IFeatureLayer (if it in fact implements IFeatureLayer). –  Sasa Ivetic Mar 28 '11 at 18:54
    
It's a desktop tool working with an iWorkspace object based on an SDE service. –  Seth P. Mar 28 '11 at 20:16
add comment
    public static string GetServerNameForLayer(ILayer pLayer)
    {
        try
        {
            if (pLayer == null) return "";
            ESRI.ArcGIS.Carto.IDataLayer pDataLayer = pLayer as IDataLayer; // QI 
            if (pDataLayer == null) return "";
            ESRI.ArcGIS.Geodatabase.IDatasetName pDatasetName = pDataLayer.DataSourceName as IDatasetName; ; // QI 
            if (pDatasetName == null) return "";
            ESRI.ArcGIS.Carto.IFeatureLayer pFeatLayer = pLayer as IFeatureLayer; // QI 
            if (pLayer == null) return "";
            ESRI.ArcGIS.Geodatabase.IDataset pDataset = pFeatLayer as IDataset; // QI 
            if (pDataset == null) return "";
            if (pDatasetName.WorkspaceName.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
            {
                if (pDataset.Category == "SDE Feature Class")
                {
                    // Get the SERVER, DATABASE, USER, VERSION
                    ESRI.ArcGIS.esriSystem.IPropertySet2 pConnProps = pDataset.Workspace.ConnectionProperties as IPropertySet2;
                    return Convert.ToString(pConnProps.GetProperty("SERVER"));
                }
                return "";
            }               
            return "";
        }
        catch (Exception ex)
        {
            string strErrMsg = "ArcMapFeatureUtils::" + System.Reflection.MethodBase.GetCurrentMethod().ReflectedType.Name;
            string crlf = "\r";
            strErrMsg = strErrMsg + " Error: " + ex.Message + crlf + ex.ToString() + crlf;
            //System.Windows.Forms.MessageBox.Show(strErrMsg, "Error");
            return "";
        }
    }
share|improve this answer
1  
This seems unnecessarily complex and doesn't answer the question which is looking for the table name, not the server name. –  blah238 Jan 20 '12 at 23:47
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.