namespace WindowsApplication1 { public partial class Form1 : Form { private IMapDocument pMapDocument = null; private IMap pMap = null; private IEnumLayer pEnumLayer = null; private IFeatureLayer pFeatureLayer = null; private IDataLayer2 pDataLayer = null; private IDatasetName pDatasetName = null; private IWorkspace pWorkspace = null; private IWorkspaceName pWorkspaceName = null; private IUID pUID = null; private string p = "password"; public Form1() { InitializeComponent(); } private void Form1_Shown(object sender, EventArgs e) { Application.DoEvents(); Start("", true); } private void Start(string sPath, bool inicializando) { bool saveInsideLayer = true; int i; string mxd; IPropertySet pConnectionProperties = new PropertySetClass(); if (sPath == "") { saveInsideLayer = false; sPath = @"C:\layer.mxd"; } try { pMapDocument = new MapDocumentClass(); if (pMapDocument.get_IsPresent(sPath)) { pMapDocument.Open(sPath, ""); } for (i = 0; i < pMapDocument.MapCount; i++) { pMap = pMapDocument.get_Map(i); pUID = new UIDClass(); pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}"; pEnumLayer = pMap.get_Layers((UID)pUID, true); pEnumLayer.Reset(); pFeatureLayer = (IFeatureLayer)pEnumLayer.Next(); while (pFeatureLayer != null) { if (pFeatureLayer.DataSourceType.Contains("SDE")) { pDataLayer = (IDataLayer2)pFeatureLayer; if (pDataLayer.DataSourceName is IDatasetName) { pDatasetName = (IDatasetName)pDataLayer.DataSourceName; pWorkspaceName = pDatasetName.WorkspaceName; if (pFeatureLayer.FeatureClass != null) // why never true? { pConnectionProperties = pFeatureLayer.FeatureClass.ExtensionProperties; } pWorkspaceName.ConnectionProperties = pConnectionProperties; } } pFeatureLayer = (IFeatureLayer)pEnumLayer.Next(); } txtServidor.Text = pConnectionProperties.GetProperty("SERVER").ToString(); txtPorta.Text = pConnectionProperties.GetProperty("INSTANCE").ToString(); txtDatabase.Text = pConnectionProperties.GetProperty("DATABASE").ToString(); txtUsuario.Text = pConnectionProperties.GetProperty("USERNAME").ToString(); txtSenha.Text = pConnectionProperties.GetProperty("PASSWORD").ToString(); if (saveInsideLayer) { pMapDocument.ReplaceContents((IMxdContents)pMap); pMapDocument.Save(); pMapDocument.Close(); } } } catch (Exception ex) { throw new Exception("Unable to run the code", ex); } } } }
if (pFeatureLayer.FeatureClass != null) { MessageBox.Show(pFeatureLayer.Name + " ok"); } else MessageBox.Show(pFeatureLayer.Name + " fail");
pDataLayer = (IDataLayer2)pFeatureLayer; if (pDataLayer.DataSourceName is IDatasetName) { pDatasetName = (IDatasetName)pDataLayer.DataSourceName; pWorkspaceName = pDatasetName.WorkspaceName; pConnectionProperties = pWorkspaceName.ConnectionProperties; }
pMapDocument.ReplaceContents((IMxdContents)pMap); pMapDocument.Save(true, false); //are these arguments ok? pMapDocument.Close();