2010-08-21 55 views
-2

你好朋友以下是我的查詢其中displaylist是對象如果stringBuilder有零長度有時,但在這種情況下,它給出錯誤附近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

我意思是我想短路displayLIst,如果它有零長度。 – NoviceToDotNet 2010-08-21 13:40:51

+0

你聽說過「。」嗎? – Eiko 2010-08-21 14:04:42

回答

1

問題是你插入顯示列表的價值就在接近逗號效率和性能點不同的查詢。如果顯示列表是空的,那麼你的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]...";