从SQL Server CE获取上次复制日期/时间

时间:2012-03-29 10:48:25

标签: sql sql-server windows-mobile sql-server-ce merge-replication

我在设备上安装了带有SQL Server CE 3.5的Windows Mobile 6.0应用程序。该程序从发布者 - SQL Server 2008获取最新数据。复制工作正常但我想在上次从服务器复制时向用户显示。

有谁知道我可以在哪里获取可在设备上执行的SQL以从SQL Server CE获取此信息?

所有人都非常感谢。

麦克

1 个答案:

答案 0 :(得分:1)

    /// <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);
        }
    }