2010-08-21 30 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]...";