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

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

 2008-12-06 10:15:37 来源:WEB开发网   
核心提示: 上面的是一个泛型的方法,我们可以对一个单独的Table的一个DataRow数组进行的更新,ORM: 开发自己的Data Access Application Block - Part IIII(4),代码相对还算清晰,相信对大部分人没有难度:首先照例使用DatabaseProviderFa

上面的是一个泛型的方法,我们可以对一个单独的Table的一个DataRow数组进行的更新,代码相对还算清晰,相信对大部分人没有难度:首先照例使用DatabaseProviderFactory创建泛型的DbCommandBuilder,指定SelectCommand的CommandText(Select * From TableName),通过DbCommandBuilder创建3个Command传递给DatabaseAdapter的3 个Command属性。如果用户开始了一个Transaction,则把创建的Transaction映射到3个Command上。最后调用DatabaseAdapter.Update方法实现 数据的跟新。通过DbCommandBuilder是一种很简单的方法,但是存在很大的性能问题。造成性能降低的主要原因有两个:他是使用纯文本的SQL;为了避免数据库的并发操作引起的数据不一致,它在作数据更新的时候,会逐个字段地把Dataset原始数据和数据库作比较。所以我们一边采用stored procedure来更新数据库。

private void UpdateDataUsingMappedStoredProcedure<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[];
        if (dataRows.Length == 0)
        {
          return;
        }
      }
  
      //Create the three commands of the database data adapter.
      DbCommand insertCommand = this.Connection.CreateCommand();
      DbCommand updateCommand = this.Connection.CreateCommand();
      DbCommand deleteCommand = this.Connection.CreateCommand();
  
      //Specify the command type.
      insertCommand.CommandType = CommandType.StoredProcedure;
      updateCommand.CommandType = CommandType.StoredProcedure;
      deleteCommand.CommandType = CommandType.StoredProcedure;
  
      insertCommand.UpdatedRowSource = UpdateRowSource.OutputParameters;
  
      string tableName = string.Empty;
      if (table != null)
      {
        tableName = table.TableName;
      }
      else
      {
        tableName = dataRows[0].Table.TableName;
      }
  
      //Specify the command text.
      insertCommand.CommandText = this._storedProcedureNameMapping.GetInsertStoredProcedureName(tableName);
      updateCommand.CommandText = this._storedProcedureNameMapping.GetModifyStoredProcedureName(tableName);
      deleteCommand.CommandText = this._storedProcedureNameMapping.GetDeleteStoredProcedureName(tableName);
  
      if (this._transaction != null)
      {
        insertCommand.Transaction = this._transaction;
        updateCommand.Transaction = this._transaction;
        deleteCommand.Transaction = this._transaction;
      }
  
      //Discover the parameters of the three commands.
      this.DiscoverParameters(insertCommand);
      this.DiscoverParameters(updateCommand);
      this.DiscoverParameters(deleteCommand);
  
      //Specify the Source column and source version.
      foreach (DbParameter parameter in insertCommand.Parameters)
      {
        if (parameter.Direction == ParameterDirection.ReturnValue)
        {
          continue;
        }
        parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
        parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
      }
  
      foreach (DbParameter parameter in updateCommand.Parameters)
      {
        if (parameter.Direction == ParameterDirection.ReturnValue)
        {
          continue;
        }
        parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
        parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
      }
  
      foreach (DbParameter parameter in deleteCommand.Parameters)
      {
        if (parameter.Direction == ParameterDirection.ReturnValue)
        {
          continue;
        }
        parameter.SourceColumn = this._dbParameterNameMapping.GetSourceCoulmnName(parameter.ParameterName);
        parameter.SourceVersion = this.GetSourceVersion(parameter.ParameterName);
      }
  
      //Evaluate the commands for the database adapter.
      this.DatabaseAdapter.InsertCommand = insertCommand;
      this.DatabaseAdapter.UpdateCommand = updateCommand;
      this.DatabaseAdapter.DeleteCommand = deleteCommand;
  
      if (instance is DataTable)
      {
        this.DatabaseAdapter.Update(table);
      }
  
      if (instance is DataRow[])
      {
        this.DatabaseAdapter.Update(dataRows);
      }
    }

上一页  1 2 3 4 5  下一页

Tags:ORM 开发 自己

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