locked
initialize a new mobile client device RRS feed

  • Question

  • Hi

    Im using MSF for devices(Ado.Net) on Windows mobile 6. And need a mechanism to reset/initialize a client. 

    Server: SQLServer

    Transport: WCF

    Client :SQLCE

    I was hoping to use SqlServer change tracking for this. I see two possible solutions here, which one is recomended?

    1. Crate new database on client and sync(Will eventuelly get OutOfMemoryException because change tracking will be reset, and first batch will be too large)

    2. Create new sdf file on server and transfer over http or ftp to client. But How to generate new sdf? Easy in Visual studio using wizard but is this possible to code in sync framework 1.0(Ado.Net)?


    Any other brilliant way to do this? all suggestions welcome:)

    Monday, March 12, 2012 2:05 PM

Answers

  • Figured it out. Boiled down the samples on MSDN to about 100 lines of code. Have just been doing some basic testing but seems promising. manual copy of sdf to Device and then sync new changes works good.

    Code:

    namespace myNamespace
    {
        class SdfGenerator
        {
            public void GenerateSdf()
            {            
                    Utility util = new Utility();
                    util.RecreateClientDatabase();
                    SampleSyncAgent sampleSyncAgent = new SampleSyncAgent();
                    SyncStatistics syncStatistics = sampleSyncAgent.Synchronize();             
            }
    
        }
        public class SampleSyncAgent : SyncAgent
        {
            public SampleSyncAgent()
            {
                this.LocalProvider = new SampleClientSyncProvider();
                this.RemoteProvider = new MyMobileServerSyncProvider();
    
                MyMobileServerSyncProvider a = new MyMobileServerSyncProvider();
                foreach (var syncAdapter in a.SyncAdapters)
                {
                    this.Configuration.SyncTables.Add(syncAdapter.TableName);
                }
            }
        }
    
        public class SampleClientSyncProvider : SqlCeClientSyncProvider
        {
            public SampleClientSyncProvider()
            {
                //Specify a connection string for the sample client database.
                Utility util = new Utility();
                this.ConnectionString = util.ClientConnString;
            }
        }
    
        public class Utility
        {
            private static string _clientPassword;
    
            //Get and set the client database password.
            public static string Password
            {
                get { return _clientPassword; }
                set { _clientPassword = value; }
            }
    
    
            //Have the user enter a password for the client database file.
            public void SetClientPassword(string pass)
            {
                Utility.Password = pass;
            }
    
    
            //Return the client connection string with the password.
            public string ClientConnString
            {
                get { return @"Data Source='MyMobile.sdf'; Password=" + Utility.Password; }
            }
    
    
            //Delete the client database.
            public void RecreateClientDatabase()
            {
                using (SqlCeConnection clientConn = new SqlCeConnection(this.ClientConnString))
                {
                    if (File.Exists(clientConn.Database))
                    {
                        File.Delete(clientConn.Database);
                    }
                }
    
    
                SqlCeEngine sqlCeEngine = new SqlCeEngine(this.ClientConnString);
                sqlCeEngine.CreateDatabase();
            }
        }
    }
    
    
                  

    Tuesday, March 13, 2012 2:27 PM

All replies

  • for #2, just create an empty SDF file and for each table, set the TableCreationOption to CreateNewTableOrFail
    Tuesday, March 13, 2012 1:32 AM
  • My question was a bit unprecise. What i meant is i need a new sdf with snapshot of data from server. Thanks for pointing me in right direction.

    This("http://msdn.microsoft.com/en-us/library/bb726043.aspx", Download a Snapshot of Data to a Client) is the way to go right? 

    My scenario:

    1. I sync on device, Get "Change tracking cleaned up" or other error message. I give up syncing on Device. database broken need new one.

    2. Call a WCF method to generate new database

    3. Download new SDF to Device. Then replace old broken SDF with new fresh SDF form server.

    Tuesday, March 13, 2012 10:14 AM
  • a snapshot is a one time copy of only. you cant use it to incrementally retrieve changes.
    Tuesday, March 13, 2012 10:46 AM
  • Figured it out. Boiled down the samples on MSDN to about 100 lines of code. Have just been doing some basic testing but seems promising. manual copy of sdf to Device and then sync new changes works good.

    Code:

    namespace myNamespace
    {
        class SdfGenerator
        {
            public void GenerateSdf()
            {            
                    Utility util = new Utility();
                    util.RecreateClientDatabase();
                    SampleSyncAgent sampleSyncAgent = new SampleSyncAgent();
                    SyncStatistics syncStatistics = sampleSyncAgent.Synchronize();             
            }
    
        }
        public class SampleSyncAgent : SyncAgent
        {
            public SampleSyncAgent()
            {
                this.LocalProvider = new SampleClientSyncProvider();
                this.RemoteProvider = new MyMobileServerSyncProvider();
    
                MyMobileServerSyncProvider a = new MyMobileServerSyncProvider();
                foreach (var syncAdapter in a.SyncAdapters)
                {
                    this.Configuration.SyncTables.Add(syncAdapter.TableName);
                }
            }
        }
    
        public class SampleClientSyncProvider : SqlCeClientSyncProvider
        {
            public SampleClientSyncProvider()
            {
                //Specify a connection string for the sample client database.
                Utility util = new Utility();
                this.ConnectionString = util.ClientConnString;
            }
        }
    
        public class Utility
        {
            private static string _clientPassword;
    
            //Get and set the client database password.
            public static string Password
            {
                get { return _clientPassword; }
                set { _clientPassword = value; }
            }
    
    
            //Have the user enter a password for the client database file.
            public void SetClientPassword(string pass)
            {
                Utility.Password = pass;
            }
    
    
            //Return the client connection string with the password.
            public string ClientConnString
            {
                get { return @"Data Source='MyMobile.sdf'; Password=" + Utility.Password; }
            }
    
    
            //Delete the client database.
            public void RecreateClientDatabase()
            {
                using (SqlCeConnection clientConn = new SqlCeConnection(this.ClientConnString))
                {
                    if (File.Exists(clientConn.Database))
                    {
                        File.Delete(clientConn.Database);
                    }
                }
    
    
                SqlCeEngine sqlCeEngine = new SqlCeEngine(this.ClientConnString);
                sqlCeEngine.CreateDatabase();
            }
        }
    }
    
    
                  

    Tuesday, March 13, 2012 2:27 PM