I’m trying to use a new class in SqlCe 3.5 SP2 called SqlCeChangeTracking. This class (allegedly) lets you turn on change tracking on a table, without using RDA replication or Sync Services.
Assuming you have an open SqlCeConnection, you enable change tracking on a table like this:
SqlCeChangeTracking tracker = new SqlCeChangeTracking(conn);
tracker.EnableTracking(TableName, TrackingKeyType.PrimaryKey,
TrackingOptions.All);
This appears to work, sort of. When I open the SDF file and view it in SQL Server Management Studio, the table has three additional fields: __sysChangeTxBsn, __sysInsertTxBsn and __sysTrackingContext. According to the sparse documentation, these columns (along with the __sysOCSDeletedRows system table) are used to track changes.
The problem is that these three columns always contain NULL values for all rows, no matter what I do. I can add, delete, edit etc. and those columns remain NULL no matter what (and no deleted records ever show up in __sysOCSDeletedRows).
I have found virtually no documentation on this class at all, and the promised MSDN API appears non-existent. Anybody know how to use this class successfully?
Update: I tried changing this to use TrackingKeyType.Guid, like so:
tracker.EnableTracking(TableName, TrackingKeyType.Guid,
TrackingOptions.All);
but this throws SqlCeException 29010 “The table does not have a primary key. [ Table name = EMPLOYEES ]”. This is weird, because I’m creating the table like this:
CREATE TABLE EMPLOYEES (BADGE NVARCHAR(5) PRIMARY KEY, NAME NVARCHAR(50),
DEPARTMENT NVARCHAR(10))
so that it does have a primary key (and I can see this PK when I open the SDF file in SQL Management Studio).
Update 2: If I try to enable tracking with one of the other two options (TrackingKeyType.None or TrackingKeyType.Max) the app dies instantly and vanishes without trace, even with a try/catch block around the line. Never a good sign.
I believe there is an issue when enabling the change tracking that it doesn’t start working until you close and re-open the SqlCeConnection after you call EnableTracking. Here’s some simple code that shows this, the first insert and update do nothing to the tracking columns, but after calling Close and Open on the connection, the 2nd update will be tracked.