2010-08-17 36 views
0

你好朋友,我有以下查詢,它打敗了這個目的。如何將curcit空參數縮短(SQL)

天前我問建議的排序電路是空的,我得到了使用
(@firstYrPercent is null OR first_year_percent>[email protected])這樣的否認空參數,但今天當我運行實際查詢的建議的參數,我認爲,上述想法沒用。

我收到SQL異常,所有這些參數,它是空的,它是爲那些要求苛刻的誤差值是該SQL異常被捕獲:

「參數化查詢「(@courseId INT,@ passoutYear INT, @currentBacklog INT,@性INT,@如」預計參數@currentDegeePercentage,但未提供「

下面是該查詢,請給我建議的替代方案:

string cmd = @"SELECT * FROM [tbl_students] WHERE [email protected] 
      AND branch_id IN(" + branchId + @") 
      AND (@firstYrPercent is null OR first_year_percent>[email protected]) 
      AND (@secondYrpercent is null OR second_year_percent>[email protected]) 
      AND (@thirdYrPercent is null OR third_year_percent>[email protected]) 
      AND (@finalYearpercent is null OR final_year_percent>[email protected]) 
      AND (@currentDegeePercentage is null OR current_degree_percent>[email protected]) 
      AND (@passoutYear is null OR passing_year>[email protected]) 
      AND (@currentBacklog is null OR current_backlog<[email protected]) 
      AND [email protected] 
      AND (@eGap is null OR gapin_education<[email protected]) 
      AND (@highSchoolPercentge is null OR highschool_percentage>[email protected]) 
      AND (@higherSchoolPercentage is null OR ssc_percentage>[email protected]) 
      AND (@grauationPercentage is null OR graduation_percentage>[email protected]) 
      AND (@diplomaPercentage is null OR diploma_percentage>[email protected]) 
      AND (@noOfAtkt is null OR number_of_ATKT<[email protected]) 
      AND (@date is null OR DOB>[email protected])"; 

回答

1

您需要確保在正在執行的命令上定義@currentDegeePercentage參數 - 具體而言,如果要將該參數作爲空值傳遞,請確保將該參數添加到命令中,其值爲:DBNull.Value

如果您不將該參數添加到Command.Parameters集合中,或者將其添加但將其值設置爲null(與DBNull.Value相反),那麼您將看到您看到的錯誤。

+0

首先,它不是一個存儲過程,無論您在上面做出什麼要求,我都已經完成了所有這些工作,但仍然存在錯誤。 它只給出那些有空值的參數的錯誤。而當我提供的所有參數錯誤不是。請讓我知道什麼是錯誤? – NoviceToDotNet 2010-08-17 10:22:24

+0

@NoviceToDotNet - 它不一定是存儲過程,我沒有提到sprocs。重點是,如果您構建任何形式的SQL語句來執行包含參數(無論它是存儲過程還是像這樣的特殊SQL語句) - 您必須確保您在執行的命令上定義了所有這些參數,否則您得到你所看到的錯誤。當您使用存儲過程時,例外情況是您可以使用默認/可選參數。 如果您將您的代碼發佈到您正在執行.NET中的SQL的地方,那將有所幫助。 – AdaTheDev 2010-08-17 10:40:20

0

問題不在於參數是null,而是參數丟失。檢查錯別字(它是「度」,而不是「degee」[原文如此])。

+0

不,我的情況下,它是「degee」,而不是程度。 而且我已經測試了它的所有值,當我提供所有參數與空值沒有它的工作。 – NoviceToDotNet 2010-08-17 10:18:25