3

I used the below function to get the single value using ArcObjects.

private string getApproverAlias(string checklistId, bool isDeleted)
{
    string approverAlias = "";
    var masterTable = featureWorkspace.OpenTable("TableOne");
    IQueryFilter masterTableFilter = new QueryFilterClass();
    masterTableFilter.SubFields = "ApproverAlias";
    masterTableFilter.WhereClause = "ChecklistId = '" + checklistId + "' AND IsDeleted = " + isDeleted;
    ICursor cursor = masterTable.Search(masterTableFilter, true);
    IRow row = null;
    while ((row = cursor.NextRow()) != null)
    {
        approverAlias = row.get_Value(row.Fields.FindField("ApproverAlias")).ToString();
    }
    return approverAlias;
}

I know from the above method the output is single value, but I'm using Cursor and Row to fetch the result.

Is it any other way to get the result without using the Cursor and Row in ArcObjects?

Something similar to ExecuteScalar in ADO.net like the below:

SqlConnection connection = new SqlConnection("Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password");
connection.Open();
SqlCommand command = new SqlCommand("SELECT ApproverAlias FROM TableOne WHERE ChecklistId = '" + checklistId + "' AND IsDeleted = " + isDeleted, connection
string approverAlias = command.ExecuteScalar();
connection.Close();

2 Answers 2

1

Basically you CAN run any SQL-statement towards your database, as ArcGIS does this internally also. However the question is: why should you want to do this? In particular there is no gain on using this kind of statement towards your current approach. When you´re interested in a single value of one single row you may limit the selected columns as done by SubFields = ... in combination with a recycling-cursor.

Only improvement is to simply ommit the while-loop and take the very first feature returned by the cursor:

var cursor = myTable.Search(...);
var row = cursor.NextRow();

EDIT: You should allways release the cursor either via ComReleaser-class or by calling Marshall.ReleaseComObject.

3
  • When I tried with cursor.NextFeature(); it throws the error 'ESRI.ArcGIS.Geodatabase.ICursor' does not contain a definition for 'NextFeature' and no extension method 'NextFeature' accepting a first argument of type 'ESRI.ArcGIS.Geodatabase.ICursor' could be found. But cursor.NextRow(); is exits.
    – Arulkumar
    Commented Jan 19, 2016 at 15:01
  • Sorry, updated the answer, ofc it is NextRow. Commented Jan 19, 2016 at 15:03
  • Yeah, by using the code I can skip the while loop and directly set the value to variable. So +1
    – Arulkumar
    Commented Jan 19, 2016 at 15:37
0

Normally you would return a row using a cursor as you have done. There is the GetFeatures() method and this would return a single feature but it relies on you knowing the ObjectID.

Personally I would stick what you have as you have optimised the cursor by set sub-fields. May be adding an attribute index would speed things up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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