I want to change data source information of a layer. Lets say I have a MXD file in dev env configured with dev environment datasource details like server,database,username,password and I want to use this MXD in test environment pointing to test datasource details. I have created a Add-In button. On click on this button, ArcMap Database Connection windows pops up asking database details to which layer should point in test environment. Once user add the details I capture this information in IWorkspace workspace:
//Code for popping up a window to ask user for datasource details
IPropertySet propertySet = new PropertySetClass();
propertySet.SetProperty("SERVERINSTANCE", "sde:sqlserver:devserver"); //sde:sqlserver:(servername)
propertySet.SetProperty("dbclient", "SQLServer");
propertySet.SetProperty("DATABASE", "databasename"); //(databasename)
Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.SdeWorkspaceFactory");
IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);
IWorkspace targetWorkspace = workspaceFactory.Open(propertySet, 0);
In below code, I am trying to access layer's workspace connection properties and then trying to set it. I dont know how to proceed further with the code.
//Code for trying to set layer datasource.
IDocument doc = ArcMap.Application.Document;
IMxDocument mxDoc = doc as IMxDocument;
IMap map = mxDoc.FocusMap;
ILayer layer = map.get_Layer(0);// Layer[0];
ESRI.ArcGIS.Carto.IFeatureLayer featureLayer = (ESRI.ArcGIS.Carto.IFeatureLayer)layer;
ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass = featureLayer.FeatureClass;
ESRI.ArcGIS.Geodatabase.IDataset dataSet = (ESRI.ArcGIS.Geodatabase.IDataset)featureClass;
ESRI.ArcGIS.esriSystem.IPropertySet propertySet = dataSet.Workspace.ConnectionProperties;
dataSet.Workspace.ConnectionProperties.SetProperty("Server", "testservername");
-
@Lloyd Dupont, I am trying to achieve what you were trying i guess few years ago. [link] gis.stackexchange.com/questions/22394/…jay– jay2018年05月27日 04:48:32 +00:00Commented May 27, 2018 at 4:48
1 Answer 1
I managed to do it using the below code:
private void ChangeDataSource(IWorkspace targetWorkspace)
{
IDocument doc = ArcMap.Application.Document;
IMxDocument mxDoc = doc as IMxDocument;
ESRI.ArcGIS.Carto.IMap map = mxDoc.FocusMap;
ESRI.ArcGIS.Carto.ILayer layer = map.get_Layer(0);
var dataLayer = (ESRI.ArcGIS.Carto.IDataLayer)layer;
var datasetName = (IDatasetName)dataLayer.DataSourceName;
var newWorkspaceName = (IWorkspaceName)((IDataset)targetWorkspace).FullName;
datasetName.WorkspaceName = newWorkspaceName;
dataLayer.DataSourceName = (IName)datasetName; ;
layer = (ESRI.ArcGIS.Carto.ILayer)dataLayer;
}
-
It might be safer to call IDatasetName.Disconnect, then set DataSourceName, then calling IDatasetName.Connect. I think this will help avoid issues if the layer's attribute table is open in arcmap when the command is invoked.Kirk Kuykendall– Kirk Kuykendall2018年05月28日 00:48:46 +00:00Commented May 28, 2018 at 0:48
-
@KirkKuykendall, thank you for your suggestion. I will add it to the code.jay– jay2018年05月28日 01:56:26 +00:00Commented May 28, 2018 at 1:56
-
Does this code suppose that the layer name in the database is the same in both environments?O.D.– O.D.2018年06月22日 08:58:21 +00:00Commented Jun 22, 2018 at 8:58
Explore related questions
See similar questions with these tags.