示例#1
0
        public async Task MainMethodCode()
        {
            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                using (RelationshipClass relationshipClass = geodatabase.OpenDataset <RelationshipClass>("LocalGovernment.GDB.luCodeViolationHasInspections"))
                    using (FeatureClass violationsFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.luCodeViolation"))
                    {
                        List <Row>  jeffersonAveViolations = new List <Row>();
                        QueryFilter queryFilter            = new QueryFilter {
                            WhereClause = "LOCDESC LIKE '///%Jefferson///%'"
                        };

                        // If you need to *cache* the rows retrieved from the cursor for processing later, it is important that you don't use recycling in
                        // the Search() method (i.e., useRecyclingCursor must be set to *false*).  Also, the returned rows/features cached in the list
                        // should be disposed when no longer in use so that unmanaged resources are not held on to longer than necessary.

                        using (RowCursor rowCursor = violationsFeatureClass.Search(queryFilter, false))
                        {
                            while (rowCursor.MoveNext())
                            {
                                jeffersonAveViolations.Add(rowCursor.Current);
                            }
                        }

                        foreach (Row jeffersoneAveViolation in jeffersonAveViolations)
                        {
                            IReadOnlyList <Row> relatedDestinationRows = relationshipClass.GetRowsRelatedToOriginRows(new List <long>
                            {
                                jeffersoneAveViolation.GetObjectID()
                            });

                            try
                            {
                                EditOperation editOperation = new EditOperation();
                                editOperation.Callback(context =>
                                {
                                    foreach (Row relatedDestinationRow in relatedDestinationRows)
                                    {
                                        try
                                        {
                                            relationshipClass.DeleteRelationship(jeffersoneAveViolation, relatedDestinationRow);
                                        }
                                        catch (GeodatabaseRelationshipClassException exception)
                                        {
                                            Console.WriteLine(exception);
                                        }
                                    }
                                }, relationshipClass);

                                bool editResult = editOperation.Execute();
                            }
                            finally
                            {
                                Dispose(relatedDestinationRows);
                            }
                        }

                        try
                        {
                            // If the table is non-versioned this is a no-op. If it is versioned, we need the Save to be done for the edits to be persisted.
                            bool saveResult = await Project.Current.SaveEditsAsync();
                        }
                        finally
                        {
                            Dispose(jeffersonAveViolations);
                        }
                    }
        }
示例#2
0
        public void MainMethodCode()
        {
            // Opening a Non-Versioned SQL Server instance.

            DatabaseConnectionProperties connectionProperties = new DatabaseConnectionProperties(EnterpriseDatabaseType.SQLServer)
            {
                AuthenticationMode = AuthenticationMode.DBMS,

                // Where testMachine is the machine where the instance is running and testInstance is the name of the SqlServer instance.
                Instance = @"testMachine\testInstance",

                // Provided that a database called LocalGovernment has been created on the testInstance and geodatabase has been enabled on the database.
                Database = "LocalGovernment",

                // Provided that a login called gdb has been created and corresponding schema has been created with the required permissions.
                User     = "******",
                Password = "******",
                Version  = "dbo.DEFAULT"
            };

            using (Geodatabase geodatabase = new Geodatabase(connectionProperties))
                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///%'"
                            };

                            // If you need to *cache* the rows retrieved from the cursor for processing later, it is important that you don't use recycling in
                            // the Search() method (i.e., useRecyclingCursor must be set to *false*).  Also, the returned rows/features cached in the list
                            // should be disposed when no longer in use so that unmanaged resources are not held on to longer than necessary.

                            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'"
                                };
                                Selection selection = inspectionTable.Select(filter, SelectionType.ObjectID, SelectionOption.Normal);

                                relatedOriginRows = relationshipClass.GetRowsRelatedToDestinationRows(selection.GetObjectIDs());

                                // Find out if any of 1st Notice inspections were from Jefferson Avenue.
                                FeatureClassDefinition featureClassDefinition = violationsFeatureClass.GetDefinition();
                                int  locationDescriptionIndex = featureClassDefinition.FindField("LOCDESC");
                                bool containsJeffersonAve     = relatedOriginRows.Any(row => Convert.ToString(row[locationDescriptionIndex]).Contains("Jefferson"));

                                List <long> jeffersonAveViolationObjectIds = jeffersonAveViolations.Select(row => row.GetObjectID()).ToList();

                                relatedDestinationRows = relationshipClass.GetRowsRelatedToOriginRows(jeffersonAveViolationObjectIds);

                                //Find out if any Jefferson Ave Violations have 1st Notice Inspections
                                TableDefinition tableDefinition           = inspectionTable.GetDefinition();
                                int             actionFieldIndex          = tableDefinition.FindField("ACTION");
                                bool            hasFirstNoticeInspections = relatedDestinationRows.Any(row => Convert.ToString(row[actionFieldIndex]).Contains("1st Notice"));
                            }
                            finally
                            {
                                Dispose(jeffersonAveViolations);
                                Dispose(relatedOriginRows);
                                Dispose(relatedDestinationRows);
                            }
                        }
        }