protected override async void OnClick() { //queued task open database var result = await QueuedTask.Run(() => { var filter = new QueryFilter { SubFields = "Shape", PrefixClause = "TOP 1", WhereClause = $"ObjectId = 15735", OutputSpatialReference = MapView.Active.Map.SpatialReference }; //make connection file reference var connectionFile = new DatabaseConnectionFile( new Uri("c:\\test\\connection.sde")); var geodatabase = new Geodatabase(connectionFile); var table = geodatabase.OpenDataset <Table> ("Mapworks.SDE.Wire"); //if the column I want isn't there, bail if (table.GetDefinition().FindField("Shape") < 0) { return(null); } try { using (var read = table.Search(filter, true)) { if (read.MoveNext()) { return(read.Current["Shape"]); } } } catch (Exception e) { Console.WriteLine(e); throw; } finally { table.Dispose(); geodatabase.Dispose(); } return(null); }); }
private Task ChangeUSHighwaysLayerDataConnectionAsync(FeatureLayer featureLayer, string catalogPath) { return(QueuedTask.Run(() => { CIMDataConnection currentDataConnection = featureLayer.GetDataConnection(); string connection = System.IO.Path.GetDirectoryName(catalogPath); string suffix = System.IO.Path.GetExtension(connection).ToLower(); var workspaceConnectionString = string.Empty; WorkspaceFactory wf = WorkspaceFactory.FileGDB; if (suffix == ".sde") { wf = WorkspaceFactory.SDE; var dbGdbConnection = new DatabaseConnectionFile(new Uri(connection, UriKind.Absolute)); workspaceConnectionString = new Geodatabase(dbGdbConnection).GetConnectionString(); } else { var dbGdbConnectionFile = new FileGeodatabaseConnectionPath(new Uri(connection, UriKind.Absolute)); workspaceConnectionString = new Geodatabase(dbGdbConnectionFile).GetConnectionString(); } string dataset = System.IO.Path.GetFileName(catalogPath); // provide a replace data connection method CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection() { WorkspaceConnectionString = workspaceConnectionString, WorkspaceFactory = wf, Dataset = dataset, DatasetType = esriDatasetType.esriDTFeatureClass }; featureLayer.SetDataConnection(updatedDataConnection); //For a RDBMS, it might look like this: //string connection = "C:\\Work\\temp.sde"; //Geodatabase sde = new Geodatabase(connection); //// provide a replace data connection method //CIMStandardDataConnection updatedDataConnection = new CIMStandardDataConnection(); //updatedDataConnection.WorkspaceConnectionString = sde.GetConnectionString(); //updatedDataConnection.WorkspaceFactory = WorkspaceFactory.SDE; //updatedDataConnection.Dataset = "vtest.usa.states"; //updatedDataConnection.DatasetType = esriDatasetType.esriDTFeatureClass; //// Alternatively, use Layer.FindAndReplaceWorkspacePath() ////Note: this will not allow changing the dataset name or workspace type //// ////string connection = "C:\\Work\\temp.sde"; ////Geodatabase sde = new Geodatabase(connection); ////featureLayer.FindAndReplaceWorkspacePath(((CIMStandardDataConnection)currentDataConnection).WorkspaceConnectionString, //// sde.GetConnectionString(), true); ////////////////////////////////////////////// ////Please Read //// //ok, so at this point we have a couple of bugs at 1.1 AND 1.2..... // //#1: if you switched to a Datasource that invalidates the Renderer, the Renderer does //not get invalidated in the UI //(eg You had a UniqueValueRenderer on a Field called "CATEGORY", the new datasource //does NOT have that field and so the renderer is invalid). // //#2: By default, Layers are added with a permanent cache. The cache is NOT automatically //invalidated so data (eg in the Attribute table, on the screen for draws) does NOT get //Refreshed so you have to invalidate the cache manually... //So, Bug #1 - we arbitrarily switch the Renderer to a simple renderer as a work around for that... featureLayer.SetRenderer(featureLayer.CreateRenderer(new SimpleRendererDefinition())); //Bug #2, we manually invalidate the cache featureLayer.ClearDisplayCache(); })); }