Programtically creating a NEW PGDB

389
5
11-01-2010 10:19 PM
ShreyPrasad
New Contributor
Hello friends

     how can we create a new pgdb to a dynamic location .
0 Kudos
5 Replies
ShreyPrasad
New Contributor
Hello friends

     how can we create a new pgdb to a dynamic location .

 
I use the following code

  public void Create_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();

}
0 Kudos
ShreyPrasad
New Contributor
---Export Function----
    private bool ExportFeatureClass(string mdbPath, string inputFeederName, string strInputGSSCode)
        {
            IEnumLayer pEnumLayer = default(IEnumLayer);
            ILayer pLayer = default(ILayer);
            IFeatureLayer pFLayer = default(IFeatureLayer);

            IFeatureClass pFc = default(IFeatureClass);
            IFeatureClassName pINFeatureClassName = default(IFeatureClassName);
            IDataset pDataset = default(IDataset);
            IDatasetName pInDsName = default(IDatasetName);
            IFeatureSelection pFSel = default(IFeatureSelection);
            ISelectionSet pSelSet = default(ISelectionSet);
            IFeatureClassName pFeatureClassName = default(IFeatureClassName);
            IDatasetName pOutDatasetName = default(IDatasetName);
            IWorkspaceName pWorkspaceName = default(IWorkspaceName);
            IExportOperation pExportOp = default(IExportOperation);
            int idx = 0;
            idx = 0;


            try
            {


                UID pUID = new UID();
                //the UID specifies the interface identifier (GUID)'that represents the type of layer you want returned.'in this case we want an EnumLayer containing all the FeatureLayer objects 
                pUID.Value = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E}";
                // Identifies FeatureLayer objects
                pEnumLayer = pMap.get_Layers(pUID, true);
                pLayer = pEnumLayer.Next();

                while ((pLayer != null))
                {
                    pFLayer = pLayer as IFeatureLayer;
                    pFc = pFLayer.FeatureClass;

                    //Get the FcName from the featureclass
                    pDataset = pFc as IDataset;
                    pINFeatureClassName = pDataset.FullName as IFeatureClassName;
                    pInDsName = pINFeatureClassName as IDatasetName;

                    //Get the selection set
                    pFSel = pFLayer as IFeatureSelection;
                    pSelSet = pFSel.SelectionSet;



                    if (pFSel.SelectionSet.Count > 0)
                    {
                        //Define the output feature class name
                        pFeatureClassName = new FeatureClassNameClass();
                        pOutDatasetName = pFeatureClassName as IDatasetName;

                        //Split(pOutDatasetName, ".")
                        if ((pDataset.Name.Contains(".")))
                        {

                            pOutDatasetName.Name = pDataset.Name.Substring(pDataset.Name.LastIndexOf(".") + 1, pDataset.Name.Length - pDataset.Name.LastIndexOf(".") - 1);


                        }
                        else
                        {
                          

                        }


                        pWorkspaceName = new WorkspaceNameClass();
                        //C:\Export.mdb
                        pWorkspaceName.PathName = "C:\\Export\\Export.mdb";
                        pWorkspaceName.WorkspaceFactoryProgID = "esriDataSourcesGDB.AccessWorkspaceFactory";
                        pOutDatasetName.WorkspaceName = pWorkspaceName;
                        pFeatureClassName.FeatureType = pFc.FeatureType;
                        pFeatureClassName.ShapeType = pFc.ShapeType;
                        pFeatureClassName.ShapeFieldName = pFc.ShapeFieldName;

                        //Export
                        int x = 0;
                        pExportOp = new ExportOperation();
                        pExportOp.ExportFeatureClass(pInDsName, null, pSelSet, null, pOutDatasetName as IFeatureClassName, 0);
                    }

                    pLayer = pEnumLayer.Next();
                }

                //Export table to the generated PGDB
                IPropertySet pPropSet = default(IPropertySet);
                IWorkspace pWorkspace = default(IWorkspace);
                IWorkspaceFactory pWSFactory = default(IWorkspaceFactory);
                IFeatureWorkspace pFWS = default(IFeatureWorkspace);
                ITable pTable = default(ITable);
                ISelectionSet pTabSelSet = default(ISelectionSet);
                ITableName pTableName = default(ITableName);
                ITableName pOutTableName = default(ITableName);
                IQueryFilter pQF = default(IQueryFilter);

              

                if (pExportOp == null)
                {
                    pExportOp = new ExportOperation();
                }

                pOutTableName = new TableNameClass();
                pOutDatasetName = pOutTableName as IDatasetName;
                pOutDatasetName.Name = "";
                pOutDatasetName.WorkspaceName = pWorkspaceName;
                pQF = new QueryFilter();


              
         
             



                return true;

            }
            catch (Exception ex)
            {
                return false;
                MessageBox.Show(ex.Message);

            }

        }
0 Kudos
DuncanHornby
MVP Notable Contributor
Shrey,

You could also create an IGeoProcessor object and call the create pGDB tool. Have a look at this for execute existing tools.

Duncan
0 Kudos
MichaelRobb
Occasional Contributor III
As stated above, you can call the GeoProcessor.

You could also create a PGD and add it to your build, in code, simply copy from the assembly directory to which ever directory you wish and rename.


' Create a file geodatabase workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)

' Create a file geodatabase.
Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:\Data", "California", Nothing, 0)
0 Kudos
JamesCrandall
MVP Frequent Contributor
As stated above, you can call the GeoProcessor.

You could also create a PGD and add it to your build, in code, simply copy from the assembly directory to which ever directory you wish and rename.


' Create a file geodatabase workspace factory.
Dim factoryType As Type = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory")
Dim workspaceFactory As IWorkspaceFactory = CType(Activator.CreateInstance(factoryType), IWorkspaceFactory)

' Create a file geodatabase.
Dim workspaceName As IWorkspaceName = workspaceFactory.Create("C:\Data", "California", Nothing, 0)


In addition to Mike's suggestion above, if you can also include this PGDB in any deployment package you might create and have the installer move the PGDB to the client machine during any installation process for your development.  I don't remember off-hand the exact code, but instead of hardcoding the location ("C:\Data" or wherever it sits) you would specify the application path in your assembly.

This works very well in an implementation I have where the PGDB acts as sort of a container of empty feature classes and tables that are dynamically populated and depleted per the functionality of the application.
0 Kudos