Search code examples
c#sqlado.netsqlanywheretable-lock

Catch table lock exception for sql anywhere ado.net


What is the best way, to catch a table lock exception with ado.net and iAnywhere.Data.SqlAnywhere.EF6? My problem is, that command.ExecuteNonQuery cause a SAException which contains the message, that a user has locked all rows in a table.

The problem is, that it is a general SAException, not a specific one like SATableLockException, which would be nice.

Now the question: how to detect without using the error message, that the error is a table lock exception?

Thank you very much!


Solution

  • Ok, i found a solution for this. I just cast the exception to SAException and userd the Error-Code property (thank you @tzup):

    Example:

    try
    {
        // Maybe some sa-exception occurse
    }
    catch (Exception ex)
    {
        if (ex is iAnywhere.Data.SQLAnywhere.SAException)
        {
            var saException = (iAnywhere.Data.SQLAnywhere.SAException)ex;
    
            // Only catch table locks
            if (saException.NativeError == -210 || saException.ErrorCode == -210
                 || saException.NativeError == -1281 || saException.ErrorCode == -1281)
            {
                // Table lock here!
            }
        }
        else { // do some thing else... }
    }