2014-03-06 9 views
0

이것은 내 코드입니다. db 매개 변수도 추가했지만 여전히 실행 중 오류를 표시합니다. 스칼라 변수를 선언해야 함DbCommand, 변수 오류를 선언해야 함

 DbCommand command; 
     StringBuilder query = new StringBuilder(
                 @"SELECT   isnull(UpsellService_OID,'') UpsellService_OID," + Environment.NewLine + 
                 "  isnull(ServiceName,'') ServiceName," + Environment.NewLine + 
                 "  isnull(ServiceDescription,'') ServiceDescription," + Environment.NewLine + 
                 "  isnull(Create_By,'') Create_By," + Environment.NewLine + 
                 "  isnull(Create_Date,'') Create_Date," + Environment.NewLine + 
                 "  isnull(Modify_By,'') Modify_By," + Environment.NewLine + 
                 "  isnull(Modify_Date,'') Modify_Date," + Environment.NewLine + 
                 "  isnull(Active_f,'') Active_f" + Environment.NewLine + 
                 "FROM TRGPAYROLL.ZONG.UPSELLSERVICES " + Environment.NewLine + 
                 "WHERE 1 = 1"); 
     if (!string.IsNullOrEmpty(idObject.ServiceName)) 
     { 
      query.Append(" AND ServiceName like '%' @ServiceName '%'"); 
     } 

     command = db.GetSqlStringCommand(query.ToString()); 
     if (!string.IsNullOrEmpty(idObject.ServiceName)) 
     { 
      db.AddInParameter(command, "ServiceName", DbType.String, idObject.ServiceName); 
     } 

     return command; 

답변

0

@ServiceName 변수가 SQL 문에 선언되어 있지 않습니다.

DECLARE @ServiceName AS nchar(32) 
SET @ServiceName = .... 
1

같은 그것의 beggining에 추가 뭔가 나는,

if (!string.IsNullOrEmpty(idObject.ServiceName)) 
    { 
     query.Append(" AND ServiceName like @ServiceName"); 
    } 

    command = db.GetSqlStringCommand(query.ToString()); 
    if (!string.IsNullOrEmpty(idObject.ServiceName)) 
    { 
     db.AddInParameter(command, "@ServiceName", DbType.String, "%" + idObject.ServiceName + "%"); 
    } 

와일드 카드는 매개 변수의 값에 직접 추가됩니다 이런 식으로 코드의 마지막 부분을 다시 것의 자리 동안 매개 변수는 임의의 문자열 연결에서 자유로 워야합니다. 그러나이 답변의 정확성을 확신하기 위해 누락 된 세부 사항이 많이 있습니다. 특히 나는 방법의 내부 동작만을 가정 할 수 있습니다. GetSqlStringCommandAddInParameter