IronPython SqlCommand affects rows, but doesn't save changes

138 views Asked by At

I have an Update and Insert scripts in IronPython, so when I run them via ExecuteNonQuery() it returns 1 affected row (as expected), but when I look in db (or run select query), there's no any changes.

def update(pConnection, transaction, serialName, serial_counter):
    query = """ UPDATE [EM_DEMO].[dbo].[tblCounter]
                    SET [Counter] =  @counter
                WHERE [ChiaveTabella] = @serial_column
            """;

    print 'query: ', query; # DEBUG INFO

    cmd = pConnection.CreateCommand();
    cmd.Transaction = transaction;
    cmd.CommandText = query;
    cmd.Parameters.AddWithValue('@counter', serial_counter.ToString());
    cmd.Parameters.AddWithValue('@serial_column', serialName);

    rowsAffected = cmd.ExecuteNonQuery();

    if not rowsAffected == 1:
        print 'Error during update! rows affected:', rowsAffected;
        return False;
    else:
        print 'Complete!';
        return True;

But when I run the same code from native C#: it works well! Can anybody help me: what's the problem?

1

There are 1 answers

0
Andrey Carter On BEST ANSWER

Forgot to commit changes manually (in native C# worked without commiting), and thrown exceptions, so it is necessary to transaction.Commit() after every execution.

# ...

rowsAffected = cmd.ExecuteNonQuery();

transaction.Commit();

# ...