Exists in ESENT

447 Views Asked by At

I'm trying to find out if a record exists in a table. Now, one obvious way would be to just seek for the record:

// Create correct keys for index with Api.MakeKey
Api.JetSeek(sessionId, tableId, SeekGrbit.SeekEQ);

However, this operation will set the cursor to the found record (on success), which I don't want to do. I guess I could somehow just quikly store the current record number and then set it back, but maybe there is a better solution? I couldn't find the correct method in the api.

So, is it possible to do a Exists, meaning a Seek just to check if the record exists?

By the way, I'm using .NET 4.0 with the ESENT Managed Interface 1.9.0.

2

There are 2 best solutions below

3
Yuliia Ashomok On

Firstly, get current position of cursor using JetGetRecordPosition method

public static void JetGetRecordPosition(
    JET_SESID sesid,
    JET_TABLEID tableid,
    out JET_RECPOS recpos
)

Then check if record exist as you done

Api.JetSeek(sessionId, tableId, SeekGrbit.SeekEQ);

or using boolean method TrySeek which returns true if a record matching the criteria was found.

public static bool TrySeek(
    JET_SESID sesid,
    JET_TABLEID tableid,
    SeekGrbit grbit
)

Then you need to set cursor on previous position, for doing it use Api.JetGotoPosition method with recpos you got from first method.

public static void JetGotoPosition(
    JET_SESID sesid,
    JET_TABLEID tableid,
    JET_RECPOS recpos
)

Hope it helps.

0
egray On

Another technique is to use the JetDupCursor() method to create a new JET_TABLEID that is independent of the current JET_TABLEID. That way the TrySeek() method will not alter the original cursor's position if the row is found

https://msdn.microsoft.com/en-us/library/microsoft.isam.esent.interop.api.jetdupcursor%28v=exchg.10%29.aspx?f=255&MSPPError=-2147217396