Search code examples
sqlsql-serverwindows-mobilesql-server-cemerge-replication

Get last replication date/time from SQL server CE


I have a Windows Mobile 6.0 application with SQL Server CE 3.5 on the device. The program gets the latest data from the publisher - SQL Server 2008. The replication is working fine but I want to display to the user the last time it replicated from the server.

Does anyone know where I can get the SQL that I can execute on the device to get this information from the SQL Server CE?

All help greatly appreciated.

Mike


Solution

  •     /// <summary>
        /// Get the local Datetime for last succesful synchronization
        /// If no Synchronization has happened, will return DateTime.MinValue
        /// </summary>
        /// <param name="connection">An open connection to the local database</param>
        /// <returns>The date and time for the last succesful sync</returns>
        public DateTime GetLastSuccessfulSyncTime(SqlCeConnection connection)
        {
            if (!System.IO.File.Exists(connection.Database))
                return DateTime.MinValue;
    
            if (connection.State != System.Data.ConnectionState.Open)
            {
                connection.Open();
            }
    
            var props = GetPropertiesFromSettings();
    
            using (SqlCeCommand cmd = connection.CreateCommand())
            {
                cmd.Connection = connection;
    
                cmd.CommandText = "SELECT table_name FROM information_schema.tables WHERE TABLE_NAME = @table";
                cmd.Parameters.Add("@table", SqlDbType.NVarChar, 4000);
                cmd.Parameters["@table"].Value = "__sysMergeSubscriptions";
                object obj = cmd.ExecuteScalar();
    
                if (obj == null)
                    return DateTime.MinValue;
                cmd.Parameters.Clear();
    
                cmd.CommandText = "SELECT LastSuccessfulSync FROM __sysMergeSubscriptions " +
                    "WHERE Publisher=@publisher AND PublisherDatabase=@database AND Publication=@publication";
    
                cmd.Parameters.Add("@publisher", SqlDbType.NVarChar, 4000);
                cmd.Parameters["@publisher"].Value = props.Publisher;
    
                cmd.Parameters.Add("@database", SqlDbType.NVarChar, 4000);
                cmd.Parameters["@database"].Value = props.PublisherDatabase;
    
                cmd.Parameters.Add("@publication", SqlDbType.NVarChar, 4000);
                cmd.Parameters["@publication"].Value = props.Publication;
    
                obj = cmd.ExecuteScalar();
                if (obj == null)
                    return DateTime.MinValue;
                else
                    return ((DateTime)obj);
            }
        }