Proper way to delete a feature class from Personal Geodatabase?

684
3
06-15-2012 01:46 PM
YukunXing
New Contributor III
Hello,

I'm working on an Addin where I need to check if the user specified output feature class already exists. If it does then it needs to be deleted first before the new one is created.

...
IFeatureClass outputFeatureClass=null;
IWorkspaceFactory2 wsFactory = new AccessWorkspaceFactory() as IWorkspaceFactory2;
                string fullDatasetName=gxDialog.FinalLocation.FullName.Replace(gxDialog.FinalLocation.Name,"");
                fullDatasetName = fullDatasetName.Substring(0, fullDatasetName.Length - 1);
                IFeatureWorkspace fWorkspace = wsFactory.OpenFromFile(fullDatasetName,0) as IFeatureWorkspace;
                IFeatureDataset fDataset = fWorkspace.OpenFeatureDataset(gxDialog.FinalLocation.Name);
                IFeatureClassContainer fcContainer = fDataset as IFeatureClassContainer;
try
                {
                        outputFeatureClass = fcContainer.get_ClassByName(gxDialog.Name);
                        IDataset dataset = outputFeatureClass as IDataset;
                        dataset.Delete();
                }
                catch (System.Exception ex)
                {
                        MessageBox.Show(ex.Message);
                }
              
                try
                {
                    outputFeatureClass = fDataset.CreateFeatureClass(
                        gxDialog.Name, fields, null, null, esriFeatureType.esriFTSimple, shapeFieldName, "");
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

This logic works fine if the target database is a File Geodatabase (of course the workspacefactory in that case is different). However it doesn't work with a Personal Geodatabase. If the output feature class already exists, this is the exception message I'm getting on the delete operation ("tmp" is the name of the output feature class):
"
The database engine could not lock table 'tmp_SHAPE_Index' because it is already in use by another person or process.
"

So I'm guessing the delete method on the IDataset interface doesn't do the job for a FC in Personal Geodatabase? What is the best way to delete a Personal Geodatabase feacture class? Thank you!

Yukun
0 Kudos
3 Replies
VivekPrasad
Occasional Contributor
Hi,

IDataset works perfectly for all sorts of GeoDatabases and ShapeFiles. Please have a look at the below sample. It worked very well.

Sub a()
    Dim pWsFact As IWorkspaceFactory
    Dim pWspace As IWorkspace
    Dim pEnumDataset As IEnumDataset
    Dim pFeatureClass As IFeatureClass
    Dim pDataset As IDataset
   
    Set pWsFact = New AccessWorkspaceFactory
    Set pWspace = pWsFact.OpenFromFile("D:\MajetiP\ArcObjects_Task\Data\National.mdb", 0)
   
    Set pEnumDataset = pWspace.Datasets(esriDTFeatureClass)
    Set pDataset = pEnumDataset.Next
   
    While (Not pDataset Is Nothing)
        If (pDataset.Name = "new") Then
            If (pDataset.CanDelete = True) Then
                pDataset.Delete
            End If
        End If
        Set pDataset = pEnumDataset.Next
    Wend
End Sub
0 Kudos
YukunXing
New Contributor III
Hi,

IDataset works perfectly for all sorts of GeoDatabases and ShapeFiles. Please have a look at the below sample. It worked very well.

Sub a()
    Dim pWsFact As IWorkspaceFactory
    Dim pWspace As IWorkspace
    Dim pEnumDataset As IEnumDataset
    Dim pFeatureClass As IFeatureClass
    Dim pDataset As IDataset
   
    Set pWsFact = New AccessWorkspaceFactory
    Set pWspace = pWsFact.OpenFromFile("D:\MajetiP\ArcObjects_Task\Data\National.mdb", 0)
   
    Set pEnumDataset = pWspace.Datasets(esriDTFeatureClass)
    Set pDataset = pEnumDataset.Next
   
    While (Not pDataset Is Nothing)
        If (pDataset.Name = "new") Then
            If (pDataset.CanDelete = True) Then
                pDataset.Delete
            End If
        End If
        Set pDataset = pEnumDataset.Next
    Wend
End Sub


Thank you for your replay Vara.

As stated, this method works fine if the target is a File GDB or a shapfile. But if it's a personal GDB, I ran into the excpetion
"
The database engine could not lock table 'tmp_SHAPE_Index' because it is already in use by another person or process.
"

where "tmp" is the name of the feature class to be deleted.

The only difference is that I used IFeatureClassContainer to find the feature class with the user specified name. Do you think that could be the cause? But then again, I used that for File GDB too and it worked...
0 Kudos
YukunXing
New Contributor III
Still stuck with the issue, hopefully someone has experienced the same problem and figured out a solution.

I also want to add that if I run the tool on an empty personal GDB once to create a FC, then restart ArcMap and run the tool again, the dataset in the Personal GDB can be deleted and a new one with the same name created without any problem. So long as I restart ArcMap after each run of the tool, there is no issue. But if the tool is run for a second time in the same ArcMap session, it is having trouble deleting the existing FC.

I'm on ArcGIS 10.0 and Windows 7.
0 Kudos