Exemplo n.º 1
0
        private void OnDeleted(SqlRowChangedEventArgs e)
        {
            var method = (EventHandler <SqlRowChangedEventArgs>)base.Events[Event_Deleted];

            if (method != null)
            {
                method(this, e);
            }
        }
Exemplo n.º 2
0
        private void JobContent(object state)
        {
            DbCommand cmd = null;

            try
            {
                // 因为没有锁表,所以在查出CurrentVersion后再继续查询对应的增删改可能会出现CurrentVersion不同步的情况
                // 所以在查询条件中加了范围限制
                long lastVersion = this.QueryCurrentVersion();
                if (_currentVersion == lastVersion)
                {
                    return;
                }

                cmd = _factory.CreateCommand(string.Empty);
                cmd.Connection.Open();
                var buffer = new StringBuilder();
                #region Insert
                foreach (var tableModel in _model)
                {
                    string pkWhere = CreateSqlWhere(buffer, tableModel);
                    cmd.CommandText = string.Format(Sql_InsertTracking, tableModel.MappedName, pkWhere, _currentVersion, lastVersion);
                    App.LogDebug("Sql_InsertTracking:{0}.", cmd.CommandText);
                    var e = new SqlRowChangedEventArgs(tableModel, SqlRowChangedTypes.Inserted, cmd);
                    while (e.DataReader.Read())
                    {
                        OnInserted(e);
                    }
                    e.DataReader.Close();
                }
                #endregion

                #region Update
                foreach (var tableModel in _model)
                {
                    string pkWhere = CreateSqlWhere(buffer, tableModel);
                    buffer.Length = 0;
                    foreach (var col in tableModel.Columns)
                    {
                        if (!col._Attribute.IsDbGenerated)
                        {
                            buffer.AppendFormat(Sql_UpdateTracking_Func, tableModel.MappedName, col.MappedName);
                        }
                    }
                    cmd.CommandText = string.Format(Sql_UpdateTracking, tableModel.MappedName, pkWhere, _currentVersion, lastVersion, buffer.ToString());
                    App.LogDebug("Sql_UpdateTracking:{0}.", cmd.CommandText);
                    var e = new SqlRowChangedEventArgs(tableModel, SqlRowChangedTypes.Updated, cmd);
                    while (e.DataReader.Read())
                    {
                        OnUpdated(e);
                    }
                    e.DataReader.Close();
                }
                #endregion

                #region Delete
                foreach (var tableModel in _model)
                {
                    buffer.Length = 0;
                    buffer.AppendJoin(",", tableModel.PrimaryKey.Select(t => t.MappedName));
                    cmd.CommandText = string.Format(Sql_DeleteTracking, tableModel.MappedName, _currentVersion, lastVersion, buffer.ToString());
                    App.LogDebug("Sql_DeleteTracking:{0}.", cmd.CommandText);
                    var e = new SqlRowChangedEventArgs(tableModel, SqlRowChangedTypes.Deleted, cmd);
                    while (e.DataReader.Read())
                    {
                        OnDeleted(e);
                    }
                    e.DataReader.Close();
                }
                #endregion

                _currentVersion = lastVersion;
                this.OnPropertyChanged(new PropertyChangedEventArgs("CurrentVersion"));
            }
            catch (Exception ex)
            {
                OnError(new ErrorEventArgs(ex));
            }
            finally
            {
                if (cmd != null)
                {
                    cmd.Connection.Close();
                }
            }
        }