Home AdLockOptimistic equivalent in razor pages
Reply: 1

AdLockOptimistic equivalent in razor pages

Jose Rodriguez
1#
Jose Rodriguez Published in 2017-12-03 08:41:49Z

I'm working on a .Net Razor Web Page and I want to know how to block the read registry (previously I used AdLockOptimistic in ADO) until the update is completed, my code is...

        var sqlLeeUsr = "SELECT UsrSmlla FROM tbUsr WHERE (UsrNro=@0)";
        var usr = db.QuerySingle(sqlLeeUsr, intUsrNro);
        if (usr != null)
        {
            intSecuencia = usr.UsrSmlla + 1;

            var sqlModUsr = "UPDATE tbUsr SET UsrSmlla=@0 FROM tbUsr WHERE (UsrNro=@1)";
            db.Execute(sqlModUsr, intSecuencia, intUsrNro);
        }
mjwills
2#
mjwills Reply to 2017-12-03 11:55:20Z

Rather than querying the value, then incrementing it, you should do the two operations in one. That way, each invocation will definitely get a unique value.

The code you want to execute is likely:

var sqlLeeUsr = "UPDATE tbUsr SET UsrSmlla=UsrSmlla+1
    FROM tbUsr
    OUTPUT INSERTED.UsrSmlla
    WHERE (UsrNro=@1)";
var changed = db.QuerySingle(sqlLeeUsr, intUsrNro);

This will update the table and return the new (incremented) value.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.368489 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO