views:

241

answers:

2

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.

+1  A: 

There is more documentation available in SQL Compact 3.5 SP2 books online, under the topic ms-help://MS.SSC.v35/MS.SSC.v35.EN/sscprog/html/5be071e5-41c9-4775-85d4-a41d6a370fe7.htm - download from here: http://www.microsoft.com/downloads/details.aspx?familyid=746C3A6E-FFB1-4C92-93FA-B3BA41FDE681&displaylang=en.

ErikEJ
+1  A: 

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.

using (var cmd = new SqlCeCommand("CREATE TABLE t1 (c1 int primary key, c2 int)", conn))
{
    cmd.ExecuteNonQuery();

    using (var ceCt = new SqlCeChangeTracking(conn))
    {
        ceCt.EnableTracking("t1", TrackingKeyType.PrimaryKey, TrackingOptions.All);
    }

    cmd.CommandText = "insert into t1 (c1, c2) values (1,1)";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "select __sysChangeTxBsn from t1 where c1 = 1";
    var val = cmd.ExecuteScalar();
    // This will be null since the connection has not been closed/reopened
    Console.WriteLine((val is DBNull) ? "NULL" : val);

    cmd.CommandText = "update t1 set c2 = 2 where c1 = 1";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "select __sysChangeTxBsn from t1 where c1 = 1";
    val = cmd.ExecuteScalar();
    // This will be null since the connection has not been closed/reopened
    Console.WriteLine((val is DBNull) ? "NULL" : val);

    // Recycle the connection to get change tracking working
    conn.Close();
    conn.Open();

    cmd.CommandText = "update t1 set c2 = 3 where c1 = 1";
    cmd.ExecuteNonQuery();

    cmd.CommandText = "select __sysChangeTxBsn from t1 where c1 = 1";
    val = cmd.ExecuteScalar();
    // This will be non-null and subsequent updates will increase it.
    Console.WriteLine((val is DBNull) ? "NULL" : val);
}
wnka