public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table employeeTable = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                {
                    // This will be false because the "EmployeeInfo" table is not enabled for attachments.

                    bool attachmentEnabledStatus = employeeTable.IsAttachmentEnabled();

                    using (Table inspectionTable = fileGeodatabase.OpenDataset <Table>("luCodeInspection"))
                    {
                        QueryFilter queryFilter = new QueryFilter {
                            WhereClause = "ACTION = '1st Notice'"
                        };

                        using (RowCursor cursor = inspectionTable.Search(queryFilter)) // Using Recycling.
                        {
                            // Adding some attachments to illustrate updating attachments.

                            bool hasRow = cursor.MoveNext();

                            using (Row currentRow = cursor.Current)
                            {
                                // The contentType is the MIME type indicating the type of file attached.

                                Attachment pngAttachment     = new Attachment("ImageAttachment.png", "image/png", CreateMemoryStreamFromContentsOf("geodatabaseAttachment.png"));
                                long       firstAttachmentId = currentRow.AddAttachment(pngAttachment);

                                // Note that updating the attachment using the same object used to add the attachment will fail with ArgumentException.

                                try
                                {
                                    pngAttachment.SetName("SomethingElse.png");
                                    currentRow.UpdateAttachment(pngAttachment);
                                }
                                catch (ArgumentException)
                                {
                                    //Handle exception
                                }

                                // One would have to first get the attachment before updating it.

                                IReadOnlyList <Attachment> attachments = currentRow.GetAttachments(new List <long> {
                                    firstAttachmentId
                                });

                                attachments[0].SetData(CreateMemoryStreamFromContentsOf("Sample.xml"));
                                attachments[0].SetContentType("text/xml");
                                attachments[0].SetName("XMLFile");

                                currentRow.UpdateAttachment(attachments[0]);
                            }
                        }
                    }
                }

            // 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 (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())
                        {
                            Feature rezoningUseCase = (Feature)landUseCursor.Current;

                            IReadOnlyList <Attachment> rezoningAttachments = rezoningUseCase.GetAttachments();

                            foreach (Attachment rezoningAttachment in rezoningAttachments)
                            {
                                if (!rezoningAttachment.GetName().ToLowerInvariant().Contains("rezoning"))
                                {
                                    rezoningAttachment.SetName(rezoningAttachment.GetName().Replace(".pdf", "Rezoning.pdf"));
                                    rezoningUseCase.UpdateAttachment(rezoningAttachment);
                                }
                            }
                        }
                    }
                }
        }
        private void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table inspectionTable = fileGeodatabase.OpenDataset <Table>("luCodeInspection"))
                {
                    QueryFilter queryFilter = new QueryFilter {
                        WhereClause = "ACTION = '1st Notice'"
                    };

                    using (RowCursor cursor = inspectionTable.Search(queryFilter)) // Using Recycling.
                    {
                        while (cursor.MoveNext())
                        {
                            using (Row currentRow = cursor.Current)
                            {
                                Attachment attachment = new Attachment("NewAttachment.png", "image/png", CreateMemoryStreamFromContentsOf("geodatabaseAttachment.png"));

                                long attachmentId = currentRow.AddAttachment(attachment);
                            }
                        }
                    }
                }

            // 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 (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())
                        {
                            Feature park = (Feature)parkingCursor.Current;

                            Attachment attachment = new Attachment("Sample.xml", "text/xml", CreateMemoryStreamFromContentsOf("Sample.xml"));

                            long attachmentId = park.AddAttachment(attachment);
                        }
                    }
                }
        }
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table employeeTable = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                {
                    // This will be false because the "EmployeeInfo" table is not enabled for attachments.

                    bool attachmentEnabledStatus = employeeTable.IsAttachmentEnabled();

                    using (Table inspectionTable = fileGeodatabase.OpenDataset <Table>("luCodeInspection"))
                    {
                        QueryFilter queryFilter = new QueryFilter {
                            WhereClause = "ACTION = '1st Notice'"
                        };

                        using (RowCursor cursor = inspectionTable.Search(queryFilter)) // Using Recycling.
                        {
                            // Adding some attachments to illustrate getting specific attachments.

                            bool hasRow = cursor.MoveNext();

                            using (Row currentRow = cursor.Current)
                            {
                                // The contentType is the MIME type indicating the type of file attached.

                                Attachment pngAttachment     = new Attachment("ImageAttachment.png", "image/png", CreateMemoryStreamFromContentsOf("geodatabaseAttachment.png"));
                                long       firstAttachmentId = currentRow.AddAttachment(pngAttachment);

                                Attachment textAttachment     = new Attachment("TextAttachment.txt", "text/plain", CreateMemoryStreamFromContentsOf("Sample.txt"));
                                long       secondAttachmentId = currentRow.AddAttachment(textAttachment);

                                IReadOnlyList <Attachment> allAttachments = currentRow.GetAttachments();
                                int count = allAttachments.Count; // This will be 2 provided there were no attachments before we added attachments.

                                // This will only give the attachment object for the first attachment id.

                                IReadOnlyList <Attachment> firstAttachmentOnlyList = currentRow.GetAttachments(new List <long> {
                                    firstAttachmentId
                                });

                                // This will only give the attachment object without the Data for the second attachment id.

                                IReadOnlyList <Attachment> secondAttachmentInfoOnlyList = currentRow.GetAttachments(new List <long> {
                                    secondAttachmentId
                                }, true);
                            }
                        }
                    }
                }

            // 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 (FeatureClass landUseCaseFeatureClass = geodatabase.OpenDataset <FeatureClass>("LocalGovernment.GDB.LandUseCase"))
                {
                    QueryFilter filter = new QueryFilter {
                        WhereClause = "CASETYPE = 'Rezoning'"
                    };

                    using (RowCursor landUseCursor = landUseCaseFeatureClass.Search(filter, false))
                    {
                        List <Attachment> rezoningAttachments = new List <Attachment>();

                        while (landUseCursor.MoveNext())
                        {
                            Feature rezoningUseCase = (Feature)landUseCursor.Current;

                            rezoningAttachments.AddRange(rezoningUseCase.GetAttachments());
                        }

                        foreach (Attachment attachment in rezoningAttachments)
                        {
                            // Process rezoning attachments in someway.
                        }
                    }
                }
        }
Exemple #4
0
        public void MainMethodCode()
        {
            using (Geodatabase fileGeodatabase = new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(@"C:\Data\LocalGovernment.gdb"))))
                using (Table employeeTable = fileGeodatabase.OpenDataset <Table>("EmployeeInfo"))
                {
                    // This will be false because the "EmployeeInfo" table is not enabled for attachments.

                    bool attachmentEnabledStatus = employeeTable.IsAttachmentEnabled();

                    using (Table inspectionTable = fileGeodatabase.OpenDataset <Table>("luCodeInspection"))
                    {
                        QueryFilter queryFilter = new QueryFilter {
                            WhereClause = "ACTION = '1st Notice'"
                        };

                        using (RowCursor cursor = inspectionTable.Search(queryFilter)) // Using Recycling.
                        {
                            // Adding some attachments to illustrate getting specific attachments.

                            bool hasRow = cursor.MoveNext();

                            using (Row currentRow = cursor.Current)
                            {
                                // The contentType is the MIME type indicating the type of file attached.

                                Attachment pngAttachment     = new Attachment("ImageAttachment.png", "image/png", CreateMemoryStreamFromContentsOf("geodatabaseAttachment.png"));
                                long       firstAttachmentId = currentRow.AddAttachment(pngAttachment);

                                Attachment textAttachment     = new Attachment("TextAttachment.txt", "text/plain", CreateMemoryStreamFromContentsOf("Sample.txt"));
                                long       secondAttachmentId = currentRow.AddAttachment(textAttachment);

                                // When the delete fails for a subset of the specified attachments, a mapping of attachment id and the exception thrown when
                                // the delete was attempted is returned.  In this case, it is trivial because only one attachment is being returned, but one
                                // can find out why the deletion failed from the return value.

                                IReadOnlyDictionary <long, Exception> failedToDelete = currentRow.DeleteAttachments(new List <long> {
                                    firstAttachmentId
                                });
                                if (failedToDelete.Count > 0)
                                {
                                    Console.WriteLine(failedToDelete[firstAttachmentId].Message);
                                }

                                // When one tries to delete all attachments on a row, the result becomes more useful.

                                IReadOnlyDictionary <long, Exception> anotherFailedToDeleteMapping = currentRow.DeleteAttachments();
                                foreach (var attachmentId in anotherFailedToDeleteMapping.Keys)
                                {
                                    // Process anotherFailedToDeleteMapping[attachmentId].
                                    Console.WriteLine(anotherFailedToDeleteMapping[attachmentId].Message);
                                }
                            }
                        }
                    }
                }

            // 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 (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())
                        {
                            Feature rezoningUseCase = (Feature)landUseCursor.Current;

                            IReadOnlyDictionary <long, Exception> result = rezoningUseCase.DeleteAttachments();

                            if (result.Count == 0)
                            {
                                continue;
                            }

                            // Process the exceptions in the result.
                        }
                    }
                }
        }