MS SQL Data Accuracy Problem

I have a situation where two people can work in the same order (stored in the MS SQL database) from two different computers. To prevent data loss in the case when you first need to save your copy of the order, and then a little later the second one will save a copy of it and overwrite the first one, I added a check before the lastSaved (datetime) field before saving.

The code looks something like this:

private bool orderIsChangedByOtherUser(Order localOrderCopy)
{
    // Look up fresh version of the order from the DB
    Order databaseOrder = orderService.GetByOrderId(localOrderCopy.Id);

    if (databaseOrder != null &&
        databaseOrder.LastSaved > localOrderCopy.LastSaved)
    {
        return true;
    }
    else
    {
        return false;
    }
}

This works most of the time, but I found one small error.

orderIsChangedByOtherUser false, ​​, ​​ , . lastSaved . , orderIsChangedByOtherUser , true, .

Visual Studio, databaseOrder.LastSaved localOrderCopy.LastSaved , .

datetime SQL:

, SQL Server DATETIME 3,33 (0,00033 ).

, , - , 10 .

: / datetime MS SQL, , ?

+5
6

, , , 2008 , lastSaved DATETIME2 ( DATETIME) SYSDATETIME(), .

+4

. , , . , - , , .

+2

SQL 2005 , , , 1/300 3.33 .

, , . , 3-4 .

, , , , , .

+1

, -? ( SQL 2008 RowVersion)

0

, - , #, , DateTime, - suer , , timestamps , , .

, .

0

.

"Last Saved" UPDATE. LastSaved = getdate() LastSaved ( , ID), LastSaved .

. -, DateTime . , , - .

Usually we use the automatically generated SPs for CRUD operations, and the tables usually use the “Created / LastUpdated” and “CreatedBy / LastUpdatedBy” pairs, where the dates are set on the server and the “By” values ​​are passed even if NULL is set to System_User

0
source

All Articles