How to automate a default style when loading an SQL database?

12-31-2013 12:36 PM
Regular Contributor
When I create a new personal geodatabase and load the data into ArcMap the symbols for my spatial objects are always pulled from my custom style without problem. However, when I do the same thing with an SQL database the symbols are never correct. I understand this is because the SQL layers have a fully qualified names and don't match that of the custom style. But what is the best way to approach a fix for this issue?

I would like to be able to add any SQL database to an ArcMap session and have my symbols assigned correctly to the the individual layers. Is there a way to update my custom style's layer names to match the fully qualified name of the SQL database? Is there a way to loop through the layers in the map and make sure the assigned symbol is correct and if not assign it the correct symbol from a custom style file? Any example code would be very much appreciated.

ArcGIS 10.2, C#, Windows 7, VS 2010/12
Thank You
0 Kudos
3 Replies
MVP Frequent Contributor

Here is one approach, written in VBA. You create your symbology in ArcMap and then save that to a LayerFile. You can then open that LayerFile and get the renderer and overwrite the renderer of the same layer in the map document. This works for file and personal geodatabases but I have not tested it on a SQL database.

Public Sub LoadALayerFile()
    ' Path to Layer file
    Dim sPath As String
    Let sPath = "C:\Scratch\CITIES_pop.lyr"
    ' Open layerFile
    Dim pLayerFile As ILayerFile
    Set pLayerFile = New LayerFile
    pLayerFile.Open sPath
    ' Get a handle on the layer in the LayerFile and its symbology
    Dim pLayer As ILayer
    Set pLayer = pLayerFile.Layer
    Dim pGeoFeatureLayer As IGeoFeatureLayer
    Set pGeoFeatureLayer = pLayer
    Dim pFeatureRenderer As IFeatureRenderer
    Set pFeatureRenderer = pGeoFeatureLayer.Renderer
    ' Get a handle on the layer in map that is the same as what LayerFile is using
    ' Assumed to be first in TOC
    Dim pMXD As IMxDocument
    Set pMXD = ThisDocument
    Dim pMap As IMap
    Set pMap = pMXD.FocusMap
    Set pLayer = pMap.Layer(0)
    ' Overwrite renderer
    Set pGeoFeatureLayer = pLayer
    Set pGeoFeatureLayer.Renderer = pFeatureRenderer
    'Update and refresh map
End Sub
Regular Contributor

Thank you for the interest in my problem and for the suggested solution. The one thing I see as an issue with this approach is that I would be required to include the layer files as part of my solutions installation. I'd rather not include any additional files if at all possible.

Is there no ArcObjects approach that can retrieve the symbology from the "Style" file and assign it to a layer? Or, is there a way to alter the file path of the symbology to include the fully qualified path?

0 Kudos
MVP Frequent Contributor

I have never tried to hook into the Style Manager to change the symbology of features in a layer. This sample page appears to do this. You will need to read about the interface IStyleGallery. Of cause I'm sure you are not the first to ask this question, try the Archived forums, here is one such thread.