get_datasets doesn't return all the datasets in the database

1270
9
Jump to solution
07-24-2012 01:55 PM
ShawnHolyoak
Occasional Contributor
I have code that gets all the Feature Datasets in a database in order to loop through them and find the one I want.  This code works fine in an SDE 9.2 database, but not in a version 10 SP 2 database.  I have 24 feature datasets in the database, and the user has at least select permissions on all of them, but the code only returns 3 feature datasets.  The code is very simple, shown below. What the heck is going on?

   ESRI.ArcGIS.Geodatabase.IWorkspace _ws = (ESRI.ArcGIS.Geodatabase.IWorkspace)aWorkspaceEdit;    ESRI.ArcGIS.Geodatabase.IEnumDataset _ed = _ws.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);    ESRI.ArcGIS.Geodatabase.IDataset _ds = null;    while ((_ds = _ed.Next()) != null) {     if (_ds.BrowseName == _datasetName) {      ESRI.ArcGIS.Geodatabase.IEnumDataset _dsed = _ds.Subsets;      ESRI.ArcGIS.Geodatabase.IDataset _dsfc = null;      while ((_dsfc = _dsed.Next()) != null) {       if (_dsfc.BrowseName == aFeatureClassName) {        _fc = (ESRI.ArcGIS.Geodatabase.IFeatureClass)_dsfc;        break;       }      }     }     if (_fc == null) {      _ds = _ed.Next();     } else {      break;     }    }    _ed.Reset();
0 Kudos
1 Solution

Accepted Solutions
sapnas
by
Occasional Contributor III
Yes. You want a reference of one feature class not all feature classes in the feature dataset. If a feature dataset contains 20 feature class your logic is creating 20 feature class objects in memory although you are not using it. IDatasetName object supply object name info and not the object itself. When you debug your logic you may notice a delay on _ds.Subsets and _ws.get_Datasets statements. I modified your code to use IDatasetName. If you prefer less code then use IWorkspace2.get_NameExists method  instead of iterating through the datasetnames.

ESRI.ArcGIS.Geodatabase.IEnumDatasetName _ed = _ws.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);             ESRI.ArcGIS.Geodatabase.IDatasetName _ds = null;             IFeatureClass _fc = null;                          while ((_ds = _ed.Next()) != null)             {                 if (_ds.Name==_datasetName)                 {                     ESRI.ArcGIS.Geodatabase.IEnumDatasetName _dsed = _ds.SubsetNames;                     ESRI.ArcGIS.Geodatabase.IDatasetName _dsfc = null;                     while ((_dsfc = _dsed.Next()) != null)                     {                         if (_dsfc.Name==aFeatureClassName)                        {                             IName name = _dsfc as IName;                             IDataset dataset = name.Open() as IDataset;                             _fc = (ESRI.ArcGIS.Geodatabase.IFeatureClass)dataset;                             break;                         }                     }                 }                 if (_fc != null)                 {                                          break;                 }             }             _ed.Reset();

View solution in original post

0 Kudos
9 Replies
sapnas
by
Occasional Contributor III
Are you able to view those feature datasets from arccatalog with the  workspace connection utilized in your code?
0 Kudos
ShawnHolyoak
Occasional Contributor
Are you able to view those feature datasets from arccatalog with the  workspace connection utilized in your code?


Yes.  Plus a few extraneous characters to meet the minimum reply length, ESRI.
0 Kudos
sapnas
by
Occasional Contributor III
The feature dataset retrieved by if (_fc == null) {
_ds = _ed.Next();
} logic is overwritten by next feature datasetwhile ((_ds = _ed.Next()) != null). Did you intend to skip the feature dataset in your logic? if not

Replace

if (_fc == null) {
_ds = _ed.Next();
}
else {
break;
}

with

if(_fc!=null)
{
break;
}
0 Kudos
ShawnHolyoak
Occasional Contributor
The feature dataset retrieved by if (_fc == null) {
_ds = _ed.Next();
} logic is overwritten by next feature datasetwhile ((_ds = _ed.Next()) != null). Did you intend to skip the feature dataset in your logic? if not

Replace

if (_fc == null) {
_ds = _ed.Next();
}
else {
break;
}

with

if(_fc!=null)
{
break;
}


Not sure why any of that code is in there to be honest.  I made your recommended changes, as you're correct, it was skipping some feature datasets.  Unfortunately, I'm still short one dataset that the user has full select, insert, update, and delete privileges on.
0 Kudos
sapnas
by
Occasional Contributor III
I was able to get all the feature datasets with the same logic. BTW never use IEnumDataset  if you intend to just iterate through the objects to retrieve the objectname. It not only slows down the proces it also causes memory leak. Use IEnumDatasetName instead. You can use below logic to construct featureclass from IDatasetName.

 IName name = datasetName as IName;
IDataset _dsfc = name.Open() as IDataset;
_fc = (ESRI.ArcGIS.Geodatabase.IFeatureClass)_dsfc;
0 Kudos
ShawnHolyoak
Occasional Contributor
I was able to get all the feature datasets with the same logic. BTW never use IEnumDataset  if you intend to just iterate through the objects to retrieve the objectname. It not only slows down the proces it also causes memory leak. Use IEnumDatasetName instead. You can use below logic to construct featureclass from IDatasetName.

 IName name = datasetName as IName;
IDataset _dsfc = name.Open() as IDataset;
_fc = (ESRI.ArcGIS.Geodatabase.IFeatureClass)_dsfc;


Thanks for the suggestions.  I'm unclear, though, about your suggested approach.  I don't want to get the object name.  I need to get a reference to a specific feature class to then edit it.  I see that your code gives me a feature class at the end; what object type is "datasetName" in your code?
0 Kudos
sapnas
by
Occasional Contributor III
Yes. You want a reference of one feature class not all feature classes in the feature dataset. If a feature dataset contains 20 feature class your logic is creating 20 feature class objects in memory although you are not using it. IDatasetName object supply object name info and not the object itself. When you debug your logic you may notice a delay on _ds.Subsets and _ws.get_Datasets statements. I modified your code to use IDatasetName. If you prefer less code then use IWorkspace2.get_NameExists method  instead of iterating through the datasetnames.

ESRI.ArcGIS.Geodatabase.IEnumDatasetName _ed = _ws.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);             ESRI.ArcGIS.Geodatabase.IDatasetName _ds = null;             IFeatureClass _fc = null;                          while ((_ds = _ed.Next()) != null)             {                 if (_ds.Name==_datasetName)                 {                     ESRI.ArcGIS.Geodatabase.IEnumDatasetName _dsed = _ds.SubsetNames;                     ESRI.ArcGIS.Geodatabase.IDatasetName _dsfc = null;                     while ((_dsfc = _dsed.Next()) != null)                     {                         if (_dsfc.Name==aFeatureClassName)                        {                             IName name = _dsfc as IName;                             IDataset dataset = name.Open() as IDataset;                             _fc = (ESRI.ArcGIS.Geodatabase.IFeatureClass)dataset;                             break;                         }                     }                 }                 if (_fc != null)                 {                                          break;                 }             }             _ed.Reset();
0 Kudos
ShawnHolyoak
Occasional Contributor
Yes. You want a reference of one feature class not all feature classes in the feature dataset. If a feature dataset contains 20 feature class your logic is creating 20 feature class objects in memory although you are not using it. IDatasetName object supply object name info and not the object itself. When you debug your logic you may notice a delay on _ds.Subsets and _ws.get_Datasets statements. I modified your code to use IDatasetName. If you prefer less code then use IWorkspace2.get_NameExists method  instead of iterating through the datasetnames.

ESRI.ArcGIS.Geodatabase.IEnumDatasetName _ed = _ws.get_DatasetNames(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureDataset);
            ESRI.ArcGIS.Geodatabase.IDatasetName _ds = null;
            IFeatureClass _fc = null;
            
            while ((_ds = _ed.Next()) != null)
            {
                if (_ds.Name==_datasetName)
                {
                    ESRI.ArcGIS.Geodatabase.IEnumDatasetName _dsed = _ds.SubsetNames;
                    ESRI.ArcGIS.Geodatabase.IDatasetName _dsfc = null;
                    while ((_dsfc = _dsed.Next()) != null)
                    {
                        if (_dsfc.Name==aFeatureClassName)                        {
                            IName name = _dsfc as IName;
                            IDataset dataset = name.Open() as IDataset;
                            _fc = (ESRI.ArcGIS.Geodatabase.IFeatureClass)dataset;
                            break;
                        }
                    }
                }
                if (_fc != null)
                {
                    
                    break;
                }
            }
            _ed.Reset();


That not only helps improve my code overall, it works and gives me all of my feature datasets.  I've accepted your answer.  Thanks for all of your help in resolving this!
0 Kudos
sapnas
by
Occasional Contributor III
Glad I could help!
0 Kudos