Change Feature Layer Data Connection Source Directly Through Enterprise GDB

1130
1
Jump to solution
02-26-2021 04:54 PM
DerekSalinas
New Contributor III

Good Afternoon,

So I currently have a button that adds a FeatureLayer to my map and redirects the .lyr data source to a featureclass via an SDE connection file. I would like to be able to redirect the data source directly to the Enterprise GDB since i have the necessary login credentials. What would i have to change in the example below to include the login credentials and server name to the connection info? 

internal class Menu1_button4 : Button
    {
        protected override void OnClick()
        {
            if (MapView.Active?.Map == null) return;
            QueuedTask.Run(() =>
            {

// add lyr/lyrx to map: This portion contains the Symbology of the added FeatureClass
                var featureLayer = (FeatureLayer)LayerFactory.Instance.CreateLayer(new Uri(@"FeatureLayer File"), MapView.Active.Map, 0, "Displayed Name");

                CIMDataConnection currentDataConnection = featureLayer.GetDataConnection();

                CIMStandardDataConnection updatedDataConnection = null;
                WorkspaceFactory wf = WorkspaceFactory.SDE;

                var dbGdbConnection = new DatabaseConnectionFile(new Uri(@"sde connection file path"));

// provide a replacement data connection method

                updatedDataConnection = new CIMStandardDataConnection()
                {
                    WorkspaceConnectionString = new Geodatabase(dbGdbConnection).GetConnectionString(),
                    WorkspaceFactory = wf,
                    Dataset = "Exact FeatureClass",
                    DatasetType = esriDatasetType.esriDTFeatureClass
                };

                featureLayer.SetDataConnection(updatedDataConnection);

                featureLayer.ClearDisplayCache();
            });
        }
    }
0 Kudos
1 Solution

Accepted Solutions
KirkKuykendall1
Occasional Contributor III

What does Geodatabase(dbGdbConnection).GetConnectionString() return?

You can hardwire that connection string and forgo the sde file.  If it has a password in it (instead of using OS Auth) it will be encrypted.  You could replace the encrypted password with an actual password - but you really shouldn't.  Better to use OS Auth.  Hardwiring passwords in software is a security hole.

When you create a new layer with a connection from an sde file, then save the layer to an lyrx, the credentials get embedded in the lyrx - there is no dependency on the sde file.  Open the lyrx file with notepad and see for yourself.

View solution in original post

1 Reply
KirkKuykendall1
Occasional Contributor III

What does Geodatabase(dbGdbConnection).GetConnectionString() return?

You can hardwire that connection string and forgo the sde file.  If it has a password in it (instead of using OS Auth) it will be encrypted.  You could replace the encrypted password with an actual password - but you really shouldn't.  Better to use OS Auth.  Hardwiring passwords in software is a security hole.

When you create a new layer with a connection from an sde file, then save the layer to an lyrx, the credentials get embedded in the lyrx - there is no dependency on the sde file.  Open the lyrx file with notepad and see for yourself.