Select to view content in your preferred language

Copy Selected records  from one PGDB to another PGDB

873
3
11-01-2010 06:40 AM
ShreyPrasad
Deactivated User
Hello All,

     I have one pgdb located at one location when the user click the export button the save dialog box will appear .The user will enter the name, one the pgdb what he requires and click the save button. At the back of this save operation only those  record which are of previous month from the current month will be exported to that newly created PGDB . But while using the following code i m getting the HRESULT exception

outputfilePath = saveFDBExport.FileName;
                pPropset = new PropertySetClass();
                pPropset.SetProperty("DATABASE", outputfilePath);
                outputWorkspaceFactory = new AccessWorkspaceFactoryClass();
                ouputWorkspace = outputWorkspaceFactory.Open(pPropset, 0);// Here I am getting the error
0 Kudos
3 Replies
AlexanderGray
Honored Contributor
Does the PGDB exist at the path specified?  If it does have you tried openFromFile?  If it doesn't you need to created the PGDB before you can open it.  If the schema is always the same I found the easiest is to create an empty template PGDB somewhere in your project and copy and rename that one when you need it.
0 Kudos
vincentLahaye
Emerging Contributor
hi,

replace "open" by "openfromfile"

ouputWorkspace = outputWorkspaceFactory.OpenFromFile(outputfilePath, 0); 


Vincent

N.B. in workspacefactory documentation you can find this note: "The "DATABASE" property is optional and is required for ArcSDE instances that manage multiple databases (for example, SQL Server)."    Not your case.
0 Kudos
ShreyPrasad
Deactivated User
Thank you friends I used the following code :

public void IWorkspaceFactory_Create_Example_Access()
        {

            path = saveFDBExport.FileName;
            outputfilePath = saveFDBExport.FileName;
            i = outputfilePath.LastIndexOf("\\");
            outpufilename = outputfilePath.Substring(i + 1);
            outputfilePath = outputfilePath.Substring(0, i);


            // create a new Access workspace factory
            IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
            // Create a workspacename with the workspace factory

            IWorkspaceName workspaceName = workspaceFactory.Create(outputfilePath, outpufilename, null, 0);
            // Cast for IName
            ESRI.ArcGIS.esriSystem.IName name = (ESRI.ArcGIS.esriSystem.IName)workspaceName;
            //Open a reference to the access workspace through the name object
            IWorkspace pGDB_workspace = (IWorkspace)name.Open();


            //Getting the Transfer
            GetTransfer();

        }

        public void GetTransfer()
        {
            try
            {
                m_pMxdocument = m_application.Document as IMxDocument;
                ////Get the first layer in the map
                pMap = m_pMxdocument.FocusMap;

                IFeatureLayer featureLayer = pMap.get_Layer(0) as IFeatureLayer;
                IFeatureClass inputFeatureClass = featureLayer.FeatureClass;
                IDataset inputDataset = (IDataset)inputFeatureClass;
                IDatasetName inputDatasetName = (IDatasetName)inputDataset.FullName;

             


                // If the user wan to Send the selected records
                // Get the layer's selection set.
                IFeatureSelection featureSelection = (IFeatureSelection)featureLayer;
                ISelectionSet selectionSet = featureSelection.SelectionSet;

                // Create a feature class name for the shapefile. This also requires creating a
                // workspace name object - since we need a workspace instance to use the field checker,
                // we might as well open it here.
                IWorkspaceFactory AccessWKSpaceFactory = new AccessWorkspaceFactoryClass();
                IWorkspace AccessWorkspace = AccessWKSpaceFactory.OpenFromFile(path, 0);
                IDataset AccessWorkspaceDataset = (IDataset)AccessWorkspace;
                IWorkspaceName workspaceName = (IWorkspaceName)AccessWorkspaceDataset.FullName;
                IFeatureClassName AccessFeatureClassName = new FeatureClassNameClass();
                IDatasetName AccessDatasetName = (IDatasetName)AccessFeatureClassName;
                AccessDatasetName.WorkspaceName = workspaceName;
                AccessDatasetName.Name = "accdt";

                // Use the IFieldChecker interface to make sure all of the field names are valid for a shapefile.
                IFieldChecker fieldChecker = new FieldCheckerClass();
                IFields shapefileFields = null;
                IEnumFieldError enumFieldError = null;
                fieldChecker.InputWorkspace = inputDataset.Workspace;
                fieldChecker.ValidateWorkspace = AccessWorkspace;
                fieldChecker.Validate(inputFeatureClass.Fields, out enumFieldError, out shapefileFields);


                // We also need to retrieve the GeometryDef from the input feature class.
                int shapeFieldPosition = inputFeatureClass.FindField(inputFeatureClass.ShapeFieldName);
                IFields inputFields = inputFeatureClass.Fields;
                IField shapeField = inputFields.get_Field(shapeFieldPosition);
                IGeometryDef geometryDef = shapeField.GeometryDef;

                // Now we can create a feature data converter.
                IFeatureDataConverter2 featureDataConverter2 = new FeatureDataConverterClass();
                IEnumInvalidObject enumInvalidObject = featureDataConverter2.ConvertFeatureClass(inputDatasetName, null, selectionSet, null, AccessFeatureClassName, geometryDef, shapefileFields, "", 1000, 0);
                // Again, checking for invalid objects would be useful at this point...
            }
            catch (Exception ex)
            {
                MessageBox.Show("" + ex);
            }


        }
0 Kudos