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

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

 2008-12-06 10:15:37 来源:WEB开发网   
核心提示: 通过传入的DataRowState创建一个DataViewRowState变量,对于DataRowState.Added和DataRowState.Modified,ORM: 开发自己的Data Access Application Block - Part IIII(3),通过Paren

通过传入的DataRowState创建一个DataViewRowState变量。对于DataRowState.Added和DataRowState.Modified,通过ParentRelations属性递归地获得并修改Parent Table的数据,然后跟新本Table,最后通过ChildRelations属性递归地获得并更新Child Table,如果发现对应的表已经更新,忽略并进入下一步。对于DataRowState.Deleted,则是一种反向的方法进行操作。而最终的Data Access 又落在了UpdateIndependentTable(updatedRows)方法上面。

private void UpdateIndependentTable(DataRow[] dataRows)
    {
      if (this._useCommandBuilder)
      {
        this.UpdateDataUsingCommandBuilder<DataRow[]>(dataRows);
      }
      else
      {
  
        this.UpdateDataUsingMappedStoredProcedure<DataRow[]>(dataRows);
      }
    }

通过._useCommandBuilder 属性判断是通过使用CommandBuilder生成Command还是通过Mapped Stored Procedure来生成Command更新数据。

private void UpdateDataUsingCommandBuilder<T>(T instance)
    {
      DataTable table = null;
      DataRow[] dataRows = null;
  
      if (instance is DataTable)
      {
        table = instance as DataTable;
      }
  
      if (instance is DataRow[])
      {
        dataRows = instance as DataRow[];
      }
  
      DbCommandBuilder commandBuilder = this._dbProviderFactory.CreateCommandBuilder();
      commandBuilder.DataAdapter = this.DatabaseAdapter;
  
      //Specify the select command of the data adapter.
      DbCommand selectComand = this._dbProviderFactory.CreateCommand();
      selectComand.CommandText = string.Format("SELECT * FROM dbo.{0}", table.TableName);
      selectComand.Connection = this.Connection;
      this.DatabaseAdapter.SelectCommand = selectComand;
  
      //Build the three commands of data adapter.
      DbCommand insertCommand = commandBuilder.GetInsertCommand();
      DbCommand updateCommand = commandBuilder.GetUpdateCommand();
      DbCommand deleteCommand = commandBuilder.GetDeleteCommand();
  
      this.DatabaseAdapter.InsertCommand = insertCommand;
      this.DatabaseAdapter.UpdateCommand = updateCommand;
      this.DatabaseAdapter.DeleteCommand = deleteCommand;
  
      //Specify the database connection for the thress commands.
      insertCommand.Connection = this.Connection;
      updateCommand.Connection = this.Connection;
      deleteCommand.Connection = this.Connection;
  
      if (this._transaction != null)
      {
        insertCommand.Transaction = this._transaction;
        updateCommand.Transaction = this._transaction;
        deleteCommand.Transaction = this._transaction;
      }
  
      if (instance is DataTable)
      {
        this.DatabaseAdapter.Update(table);
      }
  
      if (instance is DataRow[])
      {
        this.DatabaseAdapter.Update(dataRows);
      }
    }

上一页  1 2 3 4 5  下一页

Tags:ORM 开发 自己

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