Share via


Statement.getLastError Method

Definition

Retrieves the error code returned by the SQL database backend for the last SQL operation.

public:
 virtual int getLastError();
public virtual int getLastError ();
abstract member getLastError : unit -> int
override this.getLastError : unit -> int
Public Overridable Function getLastError () As Integer

Returns

The error code returned by the SQL database backend for the last SQL operation; or 0 for success.

Remarks

The following example demonstrates the getLastError method.

static void StatementGetLastError()  
{ 
    str sql, sql2; 
    UserConnection Connection = new UserConnection(); 
    Statement Statement = Connection.createStatement(); 
    boolean clear_infolog = false; 
    print "-Delete-a-non-existing-record-(valid statement)-----------"; 
    sql = "delete from zipcode where Recid = 2"; 
    new SqlStatementExecutePermission(sql).assert(); 
    Statement.executeUpdate(sql); 
    print " Error code was: ", Statement.getLastError(); 
    print " Error message was '", Statement.getLastErrorText(), "'"; 
    try 
    { 
        print "\n"; 
        print "-Delete-from-a-non-existing-table-(invalid statement)---    --"; 
        sql2 = "delete from StrangeTable07"; 
        new SqlStatementExecutePermission(sql2).assert(); 
        Statement.executeUpdate(sql2); 
    } 
    catch (exception::Error) 
    { 
        print "Exception was caught:"; 
        print " Error code was: ", Statement.getLastError(); 
        print " Error message was '", Statement.getLastErrorText(), "'"; 
    } 
    CodeAccessPermission::revertAssert(); 
    pause; 
}

Applies to