{
await QueuedTask.Run(() =>
{
try
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table table = geodatabase.OpenDataset<Table>("EmployeeInfo"))
{
QueryFilter queryFilter = new QueryFilter
{
WhereClause = "COSTCTRN = 'Information Technology'",
SubFields = "KNOWNAS, OFFICE, LOCATION",
PostfixClause = "ORDER BY OFFICE"
};
using (RowCursor rowCursor = table.Search(queryFilter, false))
{
while (rowCursor.MoveNext())
{
using (Row row = rowCursor.Current)
{
string location = Convert.ToString(row["LOCATION"]);
string knownAs = Convert.ToString(row["KNOWNAS"]);
}
}
}
}
}
catch (GeodatabaseFieldException fieldException)
{
// One of the fields in the where clause might not exist. There are multiple ways this can be handled:
// Handle error appropriately
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table table = geodatabase.OpenDataset<Table>("TableWithChineseCharacters"))
{
// This will fail with many database systems that expect Latin characters by default
string incorrectWhereClause = "颜色 = '绿'";
// Correct solution is to prepend the 'National String Prefix' to the attribute value
// For example, with SQL Server this value is 'N'
// This value is obtained using the SQLSyntax class
string nationalStringPrefix = "";
SQLSyntax sqlSyntax = geodatabase.GetSQLSyntax();
nationalStringPrefix = sqlSyntax.GetSupportedStrings(SQLStringType.NationalStringPrefix).First();
// This Where clause will work
QueryFilter queryFilter = new QueryFilter()
{
WhereClause = "颜色 = " + nationalStringPrefix + "'绿'"
};
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (FeatureClass schoolBoundaryFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.SchoolBoundary"))
{
// Using a spatial query filter to find all features which have a certain district name and lying within a given Polygon.
SpatialQueryFilter spatialQueryFilter = new SpatialQueryFilter
{
WhereClause = "DISTRCTNAME = 'Indian Prairie School District 204'",
FilterGeometry = new PolygonBuilderEx(new List<Coordinate2D>
{
new Coordinate2D(1021880, 1867396),
new Coordinate2D(1028223, 1870705),
new Coordinate2D(1031165, 1866844),
new Coordinate2D(1025373, 1860501),
new Coordinate2D(1021788, 1863810)
}).ToGeometry(),
SpatialRelationship = SpatialRelationship.Within
};
using (RowCursor indianPrairieCursor = schoolBoundaryFeatureClass.Search(spatialQueryFilter, false))
{
while (indianPrairieCursor.MoveNext())
{
using (Feature feature = (Feature)indianPrairieCursor.Current)
{
// Process the feature. For example...
Console.WriteLine(feature.GetObjectID());
}
}
}
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table enterpriseTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.piCIPCost"))
{
QueryFilter anotherQueryFilter = new QueryFilter { WhereClause = "FLOOR = 1 AND WING = 'E'" };
// For Selecting all matching entries.
using (Selection anotherSelection = enterpriseTable.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.Normal))
{
}
// This can be used to get one record which matches the criteria. No assumptions can be made about which record satisfying the criteria is selected.
using (Selection onlyOneSelection = enterpriseTable.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.OnlyOne))
{
}
// This can be used to obtain a empty selection which can be used as a container to combine results from different selections.
using (Selection emptySelection = enterpriseTable.Select(anotherQueryFilter, SelectionType.ObjectID, SelectionOption.Empty))
{
}
// If you want to select all the records in a table.
using (Selection allRecordSelection = enterpriseTable.Select(null, SelectionType.ObjectID, SelectionOption.Normal))
{
}
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.FacilitySite"))
{
List<Coordinate2D> newCoordinates = new List<Coordinate2D>
{
new Coordinate2D(1021570, 1880583),
new Coordinate2D(1028730, 1880994),
new Coordinate2D(1029718, 1875644),
new Coordinate2D(1021405, 1875397)
};
SpatialQueryFilter spatialFilter = new SpatialQueryFilter
{
WhereClause = "FCODE = 'Park'",
FilterGeometry = new PolygonBuilderEx(newCoordinates).ToGeometry(),
SpatialRelationship = SpatialRelationship.Crosses
};
// For Selecting all matching entries.
using (Selection anotherSelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.Normal))
{
}
// This can be used to get one record which matches the criteria. No assumptions can be made about which record satisfying the
// criteria is selected.
using (Selection onlyOneSelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.OnlyOne))
{
}
// This can be used to obtain an empty selction which can be used as a container to combine results from different selections.
using (Selection emptySelection = enterpriseFeatureClass.Select(spatialFilter, SelectionType.ObjectID, SelectionOption.Empty))
{
}
// If you want to select all the records in a table.
using (Selection allRecordSelection = enterpriseFeatureClass.Select(null, SelectionType.ObjectID, SelectionOption.Normal))
{
}
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri("C:\\Data\\LocalGovernment.gdb"))))
using (FeatureClass featureClass = geodatabase.OpenDataset<FeatureClass>("FacilitySite"))
{
SQLSyntax sqlSyntax = geodatabase.GetSQLSyntax();
string substringFunctionName = sqlSyntax.GetFunctionName(SQLFunction.Substring);
string upperFunctionName = sqlSyntax.GetFunctionName(SQLFunction.Upper);
string substringfunction = string.Format("{0}({1}(FCODE, 1, 6)) = 'SCHOOL'", upperFunctionName,
substringFunctionName);
QueryFilter queryFilter = new QueryFilter
{
WhereClause = substringfunction
};
using (Selection selection =
featureClass.Select(queryFilter, SelectionType.ObjectID, SelectionOption.Normal))
{
// work with the selection.
}
}
});
}
{
await QueuedTask.Run(() =>
{
string message = String.Empty;
bool modificationResult = false;
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table enterpriseTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.piCIPCost"))
{
EditOperation editOperation = new EditOperation();
editOperation.Callback(context =>
{
QueryFilter openCutFilter = new QueryFilter { WhereClause = "ACTION = 'Open Cut'" };
using (RowCursor rowCursor = enterpriseTable.Search(openCutFilter, false))
{
TableDefinition tableDefinition = enterpriseTable.GetDefinition();
int subtypeFieldIndex = tableDefinition.FindField(tableDefinition.GetSubtypeField());
while (rowCursor.MoveNext())
{
using (Row row = rowCursor.Current)
{
// In order to update the Map and/or the attribute table.
// Has to be called before any changes are made to the row.
context.Invalidate(row);
row["ASSETNA"] = "wMainOpenCut";
if (Convert.ToDouble(row["COST"]) > 700)
{
// Abandon asset if cost is higher than 700 (if that is what you want to do).
row["ACTION"] = "Open Cut Abandon";
row[subtypeFieldIndex] = 3; //subtype value for "Abandon"
}
//After all the changes are done, persist it.
row.Store();
// Has to be called after the store too.
context.Invalidate(row);
}
}
}
}, enterpriseTable);
try
{
modificationResult = editOperation.Execute();
if (!modificationResult)
{
message = editOperation.ErrorMessage;
}
}
catch (GeodatabaseException exObj)
{
message = exObj.Message;
}
}
if (!string.IsNullOrEmpty(message))
{
Debug.WriteLine(message);
}
});
}
{
await QueuedTask.Run(() =>
{
string message = String.Empty;
bool modificationResult = false;
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (FeatureClass enterpriseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.FacilitySite"))
{
FeatureClassDefinition facilitySiteDefinition = enterpriseFeatureClass.GetDefinition();
int ownTypeIndex = facilitySiteDefinition.FindField("OWNTYPE");
int areaIndex = facilitySiteDefinition.FindField(facilitySiteDefinition.GetAreaField());
EditOperation editOperation = new EditOperation();
editOperation.Callback(context =>
{
QueryFilter queryFilter = new QueryFilter
{
WhereClause = "FCODE = 'Hazardous Materials Facility' AND OWNTYPE = ' '"
};
using (RowCursor rowCursor = enterpriseFeatureClass.Search(queryFilter, false))
{
while (rowCursor.MoveNext())
{
using (Feature feature = (Feature)rowCursor.Current)
{
// In order to update the Map and/or the attribute table.
// Has to be called before any changes are made to the row
context.Invalidate(feature);
// Transfer all Hazardous Material Facilities to the City.
feature[ownTypeIndex] = "Municipal";
if (Convert.ToDouble(feature[areaIndex]) > 50000)
{
// Set the Shape of the feature to whatever you need.
List<Coordinate2D> newCoordinates = new List<Coordinate2D>
{
new Coordinate2D(1021570, 1880583),
new Coordinate2D(1028730, 1880994),
new Coordinate2D(1029718, 1875644),
new Coordinate2D(1021405, 1875397)
};
feature.SetShape(new PolygonBuilderEx(newCoordinates).ToGeometry());
}
feature.Store();
// Has to be called after the store too
context.Invalidate(feature);
}
}
}
}, enterpriseFeatureClass);
try
{
modificationResult = editOperation.Execute();
if (!modificationResult)
{
message = editOperation.ErrorMessage;
}
}
catch (GeodatabaseException exObj)
{
message = exObj.Message;
}
}
if (!string.IsNullOrEmpty(message))
{
Debug.WriteLine(message);
}
});
}
{
await QueuedTask.Run(() =>
{
string message = String.Empty;
bool deletionResult = false;
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table enterpriseTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.piCIPCost"))
{
EditOperation editOperation = new EditOperation();
editOperation.Callback(context =>
{
QueryFilter openCutFilter = new QueryFilter { WhereClause = "ACTION = 'Open Cut'" };
using (RowCursor rowCursor = enterpriseTable.Search(openCutFilter, false))
{
while (rowCursor.MoveNext())
{
using (Row row = rowCursor.Current)
{
// In order to update the Map and/or the attribute table. Has to be called before the delete.
context.Invalidate(row);
row.Delete();
}
}
}
}, enterpriseTable);
try
{
deletionResult = editOperation.Execute();
if (!deletionResult)
{
message = editOperation.ErrorMessage;
}
}
catch (GeodatabaseException exObj)
{
message = exObj.Message;
}
}
if (!string.IsNullOrEmpty(message))
{
Debug.WriteLine(message);
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (FeatureClass parkFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.Park"))
{
QueryFilter filter = new QueryFilter { WhereClause = "NUMPARKING > 0" };
using (RowCursor parkingCursor = parkFeatureClass.Search(filter, false))
{
while (parkingCursor.MoveNext())
{
using (MemoryStream stream = CreateMemoryStreamFromContentsOf("Sample.xml"))
{
Attachment attachment = new Attachment("Sample.xml", "text/xml", stream);
using (Row row = parkingCursor.Current)
{
long attachmentId = row.AddAttachment(attachment);
}
}
}
}
}
});
// Helper method to read a file into a memory stream
MemoryStream CreateMemoryStreamFromContentsOf(String fileNameWithPath)
{
MemoryStream memoryStream = new MemoryStream();
using (FileStream file = new FileStream(fileNameWithPath, FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[file.Length];
file.Read(bytes, 0, (int)file.Length);
memoryStream.Write(bytes, 0, (int)file.Length);
}
return memoryStream;
}
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (FeatureClass landUseCaseFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.LandUseCase"))
{
QueryFilter filter = new QueryFilter { WhereClause = "CASETYPE = 'Rezoning'" };
using (RowCursor landUseCursor = landUseCaseFeatureClass.Search(filter, false))
{
while (landUseCursor.MoveNext())
{
using (Feature rezoningUseCase = (Feature)landUseCursor.Current)
{
IReadOnlyList<Attachment> rezoningAttachments = rezoningUseCase.GetAttachments();
IEnumerable<Attachment> filteredAttachments = rezoningAttachments.Where(attachment => !attachment.GetName().Contains("rezoning"));
foreach (Attachment attachmentToUpdate in filteredAttachments)
{
attachmentToUpdate.SetName(attachmentToUpdate.GetName().Replace(".pdf", "Rezoning.pdf"));
rezoningUseCase.UpdateAttachment(attachmentToUpdate);
}
}
}
}
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file\\sdefile.sde"))))
using (Table inspectionTable = geodatabase.OpenDataset<Table>("luCodeInspection"))
{
QueryFilter queryFilter = new QueryFilter { WhereClause = "ACTION = '1st Notice'" };
using (RowCursor cursor = inspectionTable.Search(queryFilter, false))
{
while (cursor.MoveNext())
{
using (Row currentRow = cursor.Current)
{
IReadOnlyList<Attachment> rowAttachments = currentRow.GetAttachments(null, true);
IEnumerable<Attachment> attachments = rowAttachments.Where(attachment =>
attachment.GetContentType().Equals("application/pdf"));
IReadOnlyList<long> attachmentIDs =
attachments.Select(attachment => attachment.GetAttachmentID()) as IReadOnlyList<long>;
IReadOnlyDictionary<long, Exception> failures = currentRow.DeleteAttachments(attachmentIDs);
if (failures.Count > 0)
{
//process errors
}
}
}
}
}
});
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.luCodeViolationHasInspections"))
using (FeatureClass violationsFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.luCodeViolation"))
using (Table inspectionTable = geodatabase.OpenDataset<Table>("LocalGovernment.GDB.luCodeInspection"))
{
List<Row> jeffersonAveViolations = new List<Row>();
QueryFilter queryFilter = new QueryFilter { WhereClause = "LOCDESC LIKE '%Jefferson%'" };
using (RowCursor rowCursor = violationsFeatureClass.Search(queryFilter, false))
{
while (rowCursor.MoveNext())
{
jeffersonAveViolations.Add(rowCursor.Current);
}
}
IReadOnlyList<Row> relatedOriginRows = null;
IReadOnlyList<Row> relatedDestinationRows = null;
try
{
QueryFilter filter = new QueryFilter { WhereClause = "ACTION = '1st Notice'" };
using (Selection selection = inspectionTable.Select(filter, SelectionType.ObjectID, SelectionOption.Normal))
{
relatedOriginRows = relationshipClass.GetRowsRelatedToDestinationRows(selection.GetObjectIDs());
}
bool containsJeffersonAve = relatedOriginRows.Any(row => Convert.ToString(row["LOCDESC"]).Contains("Jefferson"));
List<long> jeffersonAveViolationObjectIds = jeffersonAveViolations.Select(row => row.GetObjectID()).ToList();
relatedDestinationRows = relationshipClass.GetRowsRelatedToOriginRows(jeffersonAveViolationObjectIds);
bool hasFirstNoticeInspections = relatedDestinationRows.Any(row => Convert.ToString(row["ACTION"]).Contains("1st Notice"));
}
finally
{
Dispose(jeffersonAveViolations);
Dispose(relatedOriginRows);
Dispose(relatedDestinationRows);
}
}
});
void Dispose(IEnumerable<Row> rows)
{
foreach (Row row in rows)
{
row.Dispose();
}
}
}
{
await QueuedTask.Run(() =>
{
using (Geodatabase geodatabase = new Geodatabase(new DatabaseConnectionFile(new Uri("path\\to\\sde\\file"))))
using (RelationshipClass relationshipClass = geodatabase.OpenDataset<RelationshipClass>("LocalGovernment.GDB.luCodeViolationHasInspections"))
using (FeatureClass violationsFeatureClass = geodatabase.OpenDataset<FeatureClass>("LocalGovernment.GDB.luCodeViolation"))
{
QueryFilter queryFilter = new QueryFilter { WhereClause = "LOCDESC LIKE '%Jefferson%'" };
using (RowCursor rowCursor = violationsFeatureClass.Search(queryFilter, false))
{
if (!rowCursor.MoveNext())
{
return;
}
using (Row jeffersonAveViolation = rowCursor.Current)
{
IReadOnlyList<Row> relatedDestinationRows = relationshipClass.GetRowsRelatedToOriginRows(new List<long>
{ jeffersonAveViolation.GetObjectID() });
try
{
EditOperation editOperation = new EditOperation();
editOperation.Callback(context =>
{
foreach (Row relatedDestinationRow in relatedDestinationRows)
{
try
{
relationshipClass.DeleteRelationship(jeffersonAveViolation, relatedDestinationRow);
}
catch (GeodatabaseRelationshipClassException exception)
{
Console.WriteLine(exception);
}
}
}, relationshipClass);
bool editResult = editOperation.Execute();
}
finally
{
foreach (Row row in relatedDestinationRows)
{
row.Dispose();
}
}
}
}
}
});
}
{
Task PartialPostingSnippetAsync(Version designVersion, FeatureClass supportStructureFeatureClass, List<long> deletedSupportStructureObjectIDs) => QueuedTask.Run(() =>
{
// Partial posting allows developers to post a subset of changes made in a version.
// One sample use case is an electric utility that uses a version to design the facilities in
// a new housing subdivision. At some point in the process, one block of new houses have been
// completed, while the rest of the subdivision remains unbuilt. Partial posting allows the user
// to post the completed work, while leaving not yet constructed features in the version to be
// posted later. Partial posting requires a branch-versioned feature service using ArcGIS
// Enterprise 10.9 and higher
// Specify a set of features that were constructed
QueryFilter constructedFilter = new QueryFilter()
{
WhereClause = "ConstructedStatus = 'True'"
};
// This selection represents the inserts and updates to the support
// structure feature class that we wish to post
using (Selection constructedSupportStructures = supportStructureFeatureClass.Select(constructedFilter, SelectionType.ObjectID,
SelectionOption.Normal))
{
// Specifying which feature deletions you wish to post is slightly trickier, since you cannot issue
// a query to fetch a set of deleted features Instead, a list of ObjectIDs must be used
using (Selection deletedSupportStructures = supportStructureFeatureClass.Select(null, SelectionType.ObjectID, SelectionOption.Empty))
{
deletedSupportStructures.Add(deletedSupportStructureObjectIDs); //deletedSupportStructureObjectIDs is defined as List<long>
// Perform the reconcile with partial post
ReconcileOptions reconcileOptions = new ReconcileOptions(); // Reconcile against Default
reconcileOptions.ConflictDetectionType = ConflictDetectionType.ByColumn;
reconcileOptions.ConflictResolutionMethod = ConflictResolutionMethod.Continue;
reconcileOptions.ConflictResolutionType = ConflictResolutionType.FavorEditVersion;
/*
PostOptions postOptions = new PostOptions();
// PartialPostSelections - Deprecated since Pro SDK 3.7. Use PartialPostOptions instead.
postOptions.PartialPostSelections = new List<Selection>()
{
constructedSupportStructures, deletedSupportStructures
};
postOptions.ServiceSynchronizationType = ServiceSynchronizationType.Synchronous;
*/
PartialPostOptions partialPostOptions = new PartialPostOptions(); // Partial post against the Default
partialPostOptions.Selections = new List<Selection>()
{
constructedSupportStructures, deletedSupportStructures
};
partialPostOptions.ServiceSynchronizationType = ServiceSynchronizationType.Synchronous;
ReconcileResult reconcileResult = designVersion.Reconcile(reconcileOptions, partialPostOptions);
// Use the reconcileResult ...
}
}
});
}
Target Platforms: Windows 11 Home, Pro, Enterprise (64 bit)