2012-02-20 2 views
0

내 문제는 내 사이트에 asp.net을 사용하고 있습니다. 도서 목록을 얻었고 모두 표시됩니다. 첫 번째 항목이 정렬되지리스트를 이용하여 내가 책 분류되지 않는이 코드를 실행할 때ASP.NET MySQL 주문 잘못된 결과 제공

public static IDataReader GetPageByCritere(
     int pageNumber, 
     int pageSize, 
     out int totalPages, 
     string critere, 
     string direction) 
    { 
     int pageLowerBound = (pageSize * pageNumber) - pageSize; 
     totalPages = 1; 
     int totalRows = GetCount(); 

     if (pageSize > 0) totalPages = totalRows/pageSize; 

     if (totalRows <= pageSize) 
     { 
      totalPages = 1; 
     } 
     else 
     { 
      int remainder; 
      Math.DivRem(totalRows, pageSize, out remainder); 
      if (remainder > 0) 
      { 
       totalPages += 1; 
      } 
     } 

     StringBuilder sqlCommand = new StringBuilder(); 
     sqlCommand.Append("SELECT * "); 
     sqlCommand.Append("FROM a_book "); 
     //sqlCommand.Append("WHERE "); 
     sqlCommand.Append("ORDER BY ?Critere ?direction "); 
     sqlCommand.Append("LIMIT ?PageSize "); 

     if (pageNumber > 1) 
     { 
      sqlCommand.Append("OFFSET ?OffsetRows "); 
     } 

     sqlCommand.Append(";"); 

     MySqlParameter[] arParams = new MySqlParameter[4]; 

     arParams[0] = new MySqlParameter("?PageSize", MySqlDbType.Int32); 
     arParams[0].Direction = ParameterDirection.Input; 
     arParams[0].Value = pageSize; 

     arParams[1] = new MySqlParameter("?OffsetRows", MySqlDbType.Int32); 
     arParams[1].Direction = ParameterDirection.Input; 
     arParams[1].Value = pageLowerBound; 

     arParams[2] = new MySqlParameter("?Critere", MySqlDbType.VarChar, 50); 
     arParams[2].Direction = ParameterDirection.Input; 
     arParams[2].Value = critere; 

     arParams[3] = new MySqlParameter("?direction", MySqlDbType.VarChar, 50); 
     arParams[3].Direction = ParameterDirection.Input; 
     arParams[3].Value = direction; 

     return MySqlHelper.ExecuteReader(
      GetReadConnectionString(), 
      sqlCommand.ToString(), 
      arParams); 
    } 
} 

, 내가 얻을 : 지금은 제목, 저자 또는 다른 속성으로이 책을 분류하려고, 난이 코드를 사용하고 있습니다 여기 exemple에 대한 제목으로 정렬 내 SqlCommand를하고 arParams : 나는 어떤 해결책을 찾지 못했습니다

{?PageSize} : 20 {?OffsetRows} : -20 {?Critere} title {?direction} DESC 

이 도와주세요 : 는

{SELECT * FROM a_book ORDER BY ?Critere ?direction LIMIT ?PageSize ;} 

arParams을하는 SqlCommand.

답변

1

불행히도이 작업은 MySql에서 작동하지만 그 후에 동작이 변경되었습니다. http://bugs.mysql.com/bug.php?id=31474을 참조하십시오. 이 점에서 Oracle 및 SQL Server처럼 작동합니다. 바라건대, 당신의 critere 문자열, 그렇지 않으면 당신은 SQL 인젝션을 방지해야합니다 사용자가 입력 또는 사용자 입력에서 공급되지 않는다 -

대신 기준에 따라하여 주문을 구축 고려할 수 있습니다. 당신의 기준은 하나의 컬럼으로 구성되어있는 경우

, 당신은 단순히 같은 작업을 수행 할 수 있습니다

sqlCommand.Append(String.Format("ORDER BY {0} {1}", Critere, direction)) 

Critere 후 원주 다중 경우는 열 방향의리스트로 정렬 기준을 통과하는 것을 고려할 수 있습니다 .

도 여기에 포함됩니다 : 당신의 응답을 Parameter in order by clause doesn't order -mysql, C#

+0

들으, 난 문자열 STR을 사용하여 고정 = "ORDER를에 의해"+ critere + ""+ 방향; sqlCommand.Append (str); 동일하게 : sqlCommand.AppendFormat ("ORDER BY {0} {1}", Critere, direction) 너무 감사드립니다. –