3

I am working with ArcObjects, in particular the ICursor Interface. I am creating a cursor and looping through a query's result set. My problem/concern is how to close or delete or release the cursor once I'm done looping through the query's result set. The query is being passed into a Sql workspace (via ISqlWorkspace interface). Code is as follows

ISqlWorkspace pSQL_WS = myWorkspace as ISqlWorkspace;                
ICursor pCursor = pSQL_WS.OpenQueryCursor(strQuery);
string cursorResults = "";
IRow pRow;

while ((pRow = pCursor.NextRow()) != null)
{
  int fc = pRow.Fields.FieldCount;
  for (int x = 0; x < fc; x++)
  {
    cursorResults += pRow.get_Value(x).ToString() + "     ";
  }
    cursorResults += Environment.NewLine;
}
_textElement.Text = Convert.ToString(cursorResults); 

I'm actually taking the results of the query and placing them in a text element. I'm thinking after the last line is where I'd want to close/delete/release my cursor, but I'm just not sure how to go about doing this.

4

To release a cursor you need to use ReleaseComObject, see this article from Esri help (9.2) which is still valid.

Edited into your code:

ISqlWorkspace pSQL_WS = myWorkspace as ISqlWorkspace;    
ICursor pCursor = pSQL_WS.OpenQueryCursor(strQuery);
string cursorResults = "";
IRow pRow;

while ((pRow = pCursor.NextRow()) != null)
{
  int fc = pRow.Fields.FieldCount;
  for (int x = 0; x < fc; x++)
  {
    cursorResults += pRow.get_Value(x).ToString() + "     ";
  }
    cursorResults += Environment.NewLine;
}
_textElement.Text = Convert.ToString(cursorResults);
//release the row and cursor locks
System.Runtime.InteropServices.Marshal.ReleaseComObject(pRow);
System.Runtime.InteropServices.Marshal.ReleaseComObject(pCursor);

Releasing these locks is important otherwise the workspace/feature class is locked for the life of the tool (and if in ArcCatalog or ArcMap persist until the application is closed) and will prevent deleting/renaming/adding fields...

To test if a feature class is already locked use ISchemaLock.GetCurrentSchemaLocks which should be tested before adding fields or attempting to rename/delete the feature class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.