3

I am writing an ArcGIS ArcMap Add-In and need to determine what database a given layer from. How can I determine this information? I attempted to use the FeatureLayer to get a FeatureClass, and then get the FeatureDataset from that. From FeatureDataset I can get the name and parse out the first component that seems to be the DB name.

However this approach seems really hacky and does not work if the layer is not part of a FeatureDataset. When I look at the layer's properties, it clearly shows the Database (see the highlighted line in the screenshot) so I think I can get the DB from a layer but I don't know what interface to use.

enter image description here

3 Answers 3

4

You can get that from the Workspace ConnectionProperties:

        var lyr = ArcMap.Document.FocusMap.get_Layer(0);

        var fc = (lyr as IFeatureLayer).FeatureClass;

        var dict = fc.GetWorkspace().ConnectionProperties.ToDictionary();

        foreach (var kvp in dict)
        {
            Trace.WriteLine(string.Format("{0}={1}", kvp.Key, kvp.Value));                
        }

Output is and the DATABASE is the property you want:

SERVER=lordnibbler
INSTANCE=sde:sqlserver:lordnibbler
DBCLIENT=sqlserver
DB_CONNECTION_PROPERTIES=lordnibbler
DATABASE=OWTest
IS_GEODATABASE=true
AUTHENTICATION_MODE=OSA
CONNPROP-REV=Rev1.0
VERSION=dbo.DEFAULT

The ToDictionary is an extension method that I picked up somewhere (probably gis.se), but so you don't have to hunt around. I don't have a c++ example:

public static Dictionary<string,object> ToDictionary(this IPropertySet propertySet)
{
    var dict = new Dictionary<string, object>();

    object propsetNames = new object[propertySet.Count - 1];
    object propsetValues = new object[propertySet.Count - 1];

    propertySet.GetAllProperties(out propsetNames, out propsetValues);

    object[] propsetNameArray = (object[])propsetNames;
    object[] propsetValueArray = (object[])propsetValues;

    for (int i = 0; i < propertySet.Count; i++)
    {
        dict.Add(propsetNameArray[i].ToString(), propsetValueArray[i].ToString());
    }

    return dict;
}
3
  • Thanks, this looks really promising. I will give this a shot once I figure out how to use IPropertySet (I am doing this in C++).
    – auujay
    Commented Nov 15, 2013 at 16:13
  • I don't know why this was down voted. This worked for me exactly as advertised, thanks!
    – auujay
    Commented Nov 15, 2013 at 17:55
  • I don't know why it was downvoted either--I even included the output. Even if it isn't the accepted, or best answer...it isn't wrong (although the ToWorkspace and ToDictionary extension methods in the answer isn't ideal without the actual methods...but I don't think its downvote-worthy). Commented Nov 15, 2013 at 18:18
4

You could cast the IFeatureClass to an IDataset. IDataset has several properties that might work to get the name of the workspace (or database).

You could try IDataset.workspace, which would give you the IWorkspace interface.

You could also try IDataset.FullName, which will give you an IName interface object, which could be cast to IWorkspaceName.

Hope that helps.

1
  • I won't down vote this as this does solve my problem in a very similar way that I did initially. However I prefer @Jay Cummins' solution since it avoids me having to parse the full name for the DB name.
    – auujay
    Commented Nov 15, 2013 at 17:57
0

Thanks Brian, that was the answer I was looking for. In my case I want to get the workspace of the selected layer so I can check it is the same as the edit workspace.

So to extend it further using VB.NET:

Dim pLayer As ILayer
pLayer = pMxDoc.SelectedLayer  'pMxDoc was created earlier

Dim selectedFL As IFeatureLayer2
selectedFL = pLayer 'QI

'Get the workspace from FL -> FC -> FD
Dim pFeatureClass As IFeatureClass
pFeatureClass = selectedFL.FeatureClass 'The FL was created earlier above

Dim pFeatureDataset As IDataset
pFeatureDataset = pFeatureClass 'QI

Dim pWorkspace As IWorkspace
pWorkspace = pFeatureDataset.Workspace

To answer the question you could cast one step further to IDatabaseConnectionInfo3:

Dim pWSConnectionInfo As IDatabaseConnectionInfo3
pWSConnectionInfo = pWorkspace 'QI

'Lets look at the workspace properties.
MsgBox("Workspace is esriWorkspaceType: " & pWorkspace.Type & vbNewLine & _
       "Database is " & pWSConnectionInfo.ConnectionServer & vbNewLine & _
       "Database name is " & pWSConnectionInfo.ConnectedDatabaseEx, _
       MsgBoxStyle.Information, "Debug check")

gives (for my Oracle sde database)

Workspace is esriWorkspaceType: 2
Database is hostname.fulldomain
Database name is actual dbasename

Naturally you'd need to be careful. Casting IDatabaseConnectionInfo3 to a FGDB won't work and will generate a runtime error unless handled. In my case it is even more complicated as the Oracle TNSNAME is an alias to the actual dbasename, and the users never use the real name only 'DMS' in their GIS applications.

Your Answer

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

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