2009-08-05 69 views
0

發生變化我使用分頁將GridView綁定到ObjectDataSource。分頁工作正常,除了排序順序根據查看結果的哪個頁面而變化。這導致項目在其他問題中重新出現在後續頁面上。我將問題追溯到我的DAL,它一次讀取一頁,然後對其進行分類。很明顯,排序會隨着結果集大小的改變而改變。這個算法有改進嗎?如果可能,我想使用數據讀取器:問題:對GridView/ObjectDataSource進行排序根據頁面

[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)] 
    public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression) 
    { 
     //DEFAULT SORT EXPRESSION 
     if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy"; 
     //CREATE THE DYNAMIC SQL TO LOAD OBJECT 
     StringBuilder selectQuery = new StringBuilder(); 
     selectQuery.Append("SELECT"); 
     if (maximumRows > 0) selectQuery.Append(" TOP " + (startRowIndex + maximumRows).ToString()); 
     selectQuery.Append(" " + Words.GetColumnNames(string.Empty)); 
     selectQuery.Append(" FROM sw_Words"); 
     string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " WHERE " + sqlCriteria; 
     selectQuery.Append(whereClause); 
     selectQuery.Append(" ORDER BY " + sortExpression); 
     Database database = Token.Instance.Database; 
     DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()); 
     //EXECUTE THE COMMAND 
     WordsCollection results = new WordsCollection(); 
     int thisIndex = 0; 
     int rowCount = 0; 
     using (IDataReader dr = database.ExecuteReader(selectCommand)) 
     { 
      while (dr.Read() && ((maximumRows < 1) || (rowCount < maximumRows))) 
      { 
       if (thisIndex >= startRowIndex) 
       { 
        Words varWords = new Words(); 
        Words.LoadDataReader(varWords, dr); 
        results.Add(varWords); 
        rowCount++; 
       } 
       thisIndex++; 
      } 
      dr.Close(); 
     } 
     return results; 
    } 

回答

0

我從閱讀MSDN中找到了解決此問題的方法。訣竅是執行完整查詢並僅返回排序總是一致的有趣子集。但是,該過程僅適用於SQL 2005.

[System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select)] 
    public static WordsCollection LoadForCriteria(string sqlCriteria, int maximumRows, int startRowIndex, string sortExpression) 
    { 
     //DEFAULT SORT EXPRESSION 
     if (string.IsNullOrEmpty(sortExpression)) sortExpression = "OrderBy"; 
     //CREATE THE DYNAMIC SQL TO LOAD OBJECT 
     StringBuilder selectQuery = new StringBuilder(); 
     selectQuery.Append("SELECT "); 
     selectQuery.Append(Words.GetColumnNames(string.Empty)); 
     selectQuery.Append(" FROM ("); 
     selectQuery.Append("SELECT "); 
     selectQuery.Append(Words.GetColumnNames(string.Empty)); 
     selectQuery.Append(", ROW_NUMBER() OVER (ORDER BY " + sortExpression + ") AS RowRank"); 
     selectQuery.Append(" FROM sw_Words) AS WordsWithRowNumbers"); 
     selectQuery.Append(" WHERE RowRank >" + startRowIndex.ToString() + " AND " + "RowRank <=" + (startRowIndex + maximumRows).ToString()); 
     string whereClause = string.IsNullOrEmpty(sqlCriteria) ? string.Empty : " AND " + sqlCriteria; 
     selectQuery.Append(whereClause); 
     Database database = Token.Instance.Database; 
     DbCommand selectCommand = database.GetSqlStringCommand(selectQuery.ToString()); 
     //EXECUTE THE COMMAND 
     WordsCollection results = new WordsCollection(); 
     int rowCount = 0; 
     using (IDataReader dr = database.ExecuteReader(selectCommand)) 
     { 
      while (dr.Read()) 
      { 
       Words varWords = new Words(); 
       Words.LoadDataReader(varWords, dr); 
       results.Add(varWords); 
       rowCount++; 
      } 
      dr.Close(); 
     } 
     return results; 
    } 
相關問題