2010-08-21 5 views
-2

안녕하세요 친구가 내 쿼리 where displaylist 경우 개체가 stringBuilder가 길이가 0 인 경우가 있지만 그 경우 잘못된 구문을 FROM 근처에 제공합니다. 어떻게이 문제를 피할 수 있습니까? 다른 형태로 이러한 문제가 발생하지 않도록 또는 뷰sql 쿼리 및 asp.net

string cmd = @"SELECT [tbl_course].course_name as Course_Name , [tbl_branch].branch_name as Branch_Name,"[email protected]" FROM [tbl_students], [tbl_course], [tbl_branch] 
         WHERE [tbl_students].course_id= @courseId 
         AND [tbl_students].branch_id IN(" + branchId + @") 
         AND (@firstYrPercent is null OR[tbl_students].first_year_percent>[email protected]) 
         AND (@secondYrpercent is null OR[tbl_students].second_year_percent>[email protected]) 
         AND (@thirdYrPercent is null OR[tbl_students].third_year_percent>[email protected]) 
         AND (@finalYearpercent is null OR[tbl_students].final_year_percent>[email protected]) 
         AND (@currentDegeePercentage is null OR[tbl_students].current_degree_percent>[email protected]) 
         AND (@passoutYear is null OR[tbl_students].passing_year>[email protected]) 
         AND (@currentBacklog is null OR[tbl_students].current_backlog<[email protected]) 
         AND [tbl_students][email protected] 
         AND (@eGap is null OR [tbl_students].gapin_education<[email protected]) 
         AND (@highSchoolPercentge is null OR[tbl_students].highschool_percentage>[email protected]) 
         AND (@higherSchoolPercentage is null OR[tbl_students].ssc_percentage>[email protected]) 
         AND (@grauationPercentage is null OR[tbl_students].graduation_percentage>[email protected]) 
         AND (@diplomaPercentage is null OR[tbl_students].diploma_percentage>[email protected]) 
         AND (@noOfAtkt is null OR[tbl_students].number_of_ATKT<[email protected]) 
         AND (@validDate is null OR[tbl_students].DOB<[email protected]) 
         AND [tbl_students].branch_i 

d=[tbl_branch].branch_id AND [tbl_students].course_id=[tbl_course].course_id"; 
+0

전 길이가 0 인 경우 표시를 짧게하고 싶습니다. – NoviceToDotNet

+0

"."에 대해 들어 보셨습니까? – Eiko

답변

1

문제는 당신이 DisplayList가의 값을 삽입 직전 쉼표의 효율과 성능 관점에서 또한 나에게 만들 수있는 최적의 솔루션을 제안하고십시오하는 경우 다른 쿼리. DisplayList가 비어있는 경우, 다음 SQL 문은 다음과 같습니다

...[tbl_branch].branch_name as Branch_Name, FROM [tbl_students]... 

그래서 하나가 dispalyList (displayList = ", [tbl_branch].branch_foo")에 쉼표를 포함하거나 SQL에 삽입 할 때, 조건문을 사용

string cmd = @"SELECT [tbl_course].course_name as Course_Name , [tbl_branch].branch_name as Branch_Name" + String.IsNullOrEmpty(displayList) ? "" : ", " + displayList + @" FROM [tbl_students]...";