How to open a cursor based on a query (not QueryDef)?

479
1
Jump to solution
04-22-2022 09:15 AM
FridjofSchmidt
Occasional Contributor

In ArcObjects it is possible to return a cursor based on a query string with ISqlWorkspace.OpenQueryCursor(myQuery). I was wondering if and how this is possible in the ArcGIS Pro SDK.

The query is in fact calling an Oracle stored procedure which returns a number based on an input number:

var myQuery = string.Format("select forstgis.wimsidutil.nextwimsid({0}) from dual", unitId);

I tried to put this into a QueryDef and use Geodatabase.Evaluate(queryDef) like this:

            var queryDef = new QueryDef
            {
                Tables = "dual",
                SubFields = string.Format("forstgis.wimsidutil.nextwimsid({0})", unitId)
            };

However, Evaluate in this case throws a COMException (HRESULT: 0x80040351).

I know there is also the ArcSDESQLExecute class, but I would prefer not to use Geoprocessing in this case.

0 Kudos
1 Solution

Accepted Solutions
FridjofSchmidt
Occasional Contributor

Found a solution based on the Database class instead of Geodatabase:

var query = string.Format("select cast(forstgis.wimsidutil.nextwimsid({0}) " +
    "as number(38,0)) id from dual", unitId);
using (var queryDesc = database.GetQueryDescription(query, "QueryLayer"))
{
    queryDesc.SetObjectIDFields("id");
    using (var table = database.OpenTable(queryDesc))
    using (var cursor = table.Search())
    {
        while (cursor.MoveNext())
        {
            using (var row = cursor.Current)
            {
                return (int)row[0];
            }
        }
    }
}

View solution in original post

0 Kudos
1 Reply
FridjofSchmidt
Occasional Contributor

Found a solution based on the Database class instead of Geodatabase:

var query = string.Format("select cast(forstgis.wimsidutil.nextwimsid({0}) " +
    "as number(38,0)) id from dual", unitId);
using (var queryDesc = database.GetQueryDescription(query, "QueryLayer"))
{
    queryDesc.SetObjectIDFields("id");
    using (var table = database.OpenTable(queryDesc))
    using (var cursor = table.Search())
    {
        while (cursor.MoveNext())
        {
            using (var row = cursor.Current)
            {
                return (int)row[0];
            }
        }
    }
}
0 Kudos