WEB开发网
开发学院数据库MSSQL Server ORM: 开发自己的Data Access Application Block -... 阅读

ORM: 开发自己的Data Access Application Block - Part IIII

 2008-12-06 10:15:37 来源:WEB开发网   
核心提示: 整个过程分3步骤,更新Deleted data.-> 更新Modified data.-> 更新Added data.真正的数据更新集中在UpdateDependentTable这样一个方法中,ORM: 开发自己的Data Access Application Block -

整个过程分3步骤,更新Deleted data.-> 更新Modified data.-> 更新Added data.真正的数据更新集中在UpdateDependentTable这样一个方法中。

private void UpdateDependentTable(DataTable table, DataRowState rowState)
    {
      DataViewRowState dataViewRowState = DataViewRowState.OriginalRows;
  
      switch (rowState)
      {
        case DataRowState.Added:
          {
            dataViewRowState = DataViewRowState.Added;
            break;
          }
        case DataRowState.Deleted:
          {
            dataViewRowState = DataViewRowState.Deleted;
            break;
          }
        case DataRowState.Modified:
          {
            dataViewRowState = DataViewRowState.ModifiedCurrent;
            break;
          }
      }
  
      if (table.GetChanges(rowState) == null)
      {
        return;
      }
  
      DataTable parentTable = null;
      DataTable childTable = null;
  
      switch (rowState)
      {
        // DataRowState.Added|DataRowState.Modified: Parent table =>child table.
        case DataRowState.Added:
        case DataRowState.Modified:
          {
            foreach (DataRelation relation in table.ParentRelations)
            {
              parentTable = relation.ParentTable;
              if (parentTable.GetChanges(rowState) == null)
              {
                continue;
              }
  
              this.UpdateDependentTable(parentTable, rowState);
            }
  
            DataRow[] updatedRows = table.Select(string.Empty, string.Empty, dataViewRowState);
            this.UpdateIndependentTable(updatedRows);
  
            foreach (DataRow row in updatedRows)
            {
              row.AcceptChanges();
            }
  
            foreach (DataRelation relation in table.ChildRelations)
            {
              childTable = relation.ChildTable;
              if (childTable.GetChanges(rowState) == null)
              {
                continue;
              }
  
              this.UpdateDependentTable(childTable, rowState);
            }
            break;
          }
        // DataRowState.Deleted: Child table => Parent table.
        case DataRowState.Deleted:
          {
            //Child Tables
            foreach (DataRelation relation in table.ChildRelations)
            {
              childTable = relation.ChildTable;
              if (childTable.GetChanges(rowState) == null)
              {
                continue;
              }
  
              this.UpdateDependentTable(childTable, rowState);
            }
  
            //Itself
            DataRow[] updatedRows = table.Select(string.Empty, string.Empty, dataViewRowState);
            this.UpdateIndependentTable(updatedRows);
  
            foreach (DataRow row in updatedRows)
            {
              row.AcceptChanges();
            }
  
            //Parent Table.
            foreach (DataRelation relation in table.ParentRelations)
            {
              parentTable = relation.ParentTable;
              if (parentTable.GetChanges(rowState) == null)
              {
                continue;
              }
  
              this.UpdateDependentTable(parentTable, rowState);
            }
          }
          break;
      }
    }

上一页  1 2 3 4 5  下一页

Tags:ORM 开发 自己

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接