2014-05-15 60 views
0

查詢不工作,我有我的C#代碼執行的查詢:爲什麼如果值爲空

protected void btnSearch_Click(object sender, EventArgs e) { 
     Conn = new SqlConnection(cString); 
     Conn.Open(); 

     theGender = slcGender.SelectedItem.Text; 

     if (slcLocation.SelectedItem.Value == "") { 
      locVal = slcLocation.SelectedItem.Value; 
     } 
     if (slcLocation.SelectedItem.Value != "") { 
      locVal = slcLocation.SelectedItem.Text; 
     } 

     if (slcSpecialty.SelectedItem.Value == "") { 
      speVal = slcSpecialty.SelectedItem.Value; 
     } 
     if (slcSpecialty.SelectedItem.Value != "") { 
      speVal = slcSpecialty.SelectedItem.Text; 
     } 

     if (slcGender.SelectedItem.Value == "") { 
      genVal = slcGender.SelectedItem.Value; 
     } 
     if (slcGender.SelectedItem.Value != "") { 
      genVal = theGender.Substring(0, 1); 
     } 

     sqlCode = 
    "DECLARE @strLocation varchar(200) 
    SET @strLocation = '" + locVal + "' 

    DECLARE @strSpecialty varchar(200) 
    SET @strSpecialty = '" + speVal + "' 

    DECLARE @strGender varchar(200) 
    SET @strGender = '" + genVal + "' 

    SELECT 
     [content_title] AS [Physician Name] 
     , CAST([content_html] AS XML).value('(root/Physicians/picture/img/@src)[1]','varchar(255)') AS [Image] 
     , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('root/Physicians/gender'))) AS [Gender] 
     , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office1/a') AS [Office1] 
     , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office2/a') AS [Office2] 
     , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office3/a') AS [Office3] 
     , CAST([content_html] AS XML).query('/root/Physicians/OfficeLocations/office4/a') AS [Office4] 
     , dbo.usp_ClearHTMLTags(CONVERT(nvarchar(600), CAST([content_html] AS XML).query('/root/Physicians/phone1'))) AS [Phone #] 
    FROM 
     [database].[dbo].[content] 
    WHERE 
     [folder_id] = '188' 
     AND 
     (content_html LIKE '%<gender>'+ @strGender+'</gender>%') 
     AND 
     (content_html LIKE '%'[email protected]+'%') 
     AND 
     (content_html LIKE '%'[email protected]+'%') 
    ORDER BY 
     [content_title]"; 

     /* EXECUTE AND DISPLAY THE DATA IN THE ASP PAGE */ 


     using(SqlCommand command = new SqlCommand(sqlCode, Conn)) { 
      command.CommandType = CommandType.Text; 

      using (SqlDataReader reader = command.ExecuteReader()) { 
       if (reader.HasRows) { 
        rptContent.DataSource = reader; 
        rptContent.DataBind(); 
       } 
       else { 
        dlo.InnerHtml = "NO RESULT"; 
       } 
      } 
     } 
    } 

該聲明是在我的ASP.net網頁三個獨立的下拉列表。我有一個問題,如果我用上面的代碼執行查詢,沒有結果會顯示,儘管應該有大約5個結果。

三個dropdownboxes如下:

enter image description here

All LocationsAll SpecialtiesAny Gender""這使得應該將下面的代碼搜索所有,如果我理解正確的價值?

(content_html LIKE '%<gender>'+ @strGender+'</gender>%') 
    AND 
    (content_html LIKE '%'[email protected]+'%') 
    AND 
    (content_html LIKE '%'[email protected]+'%') 

它的工作原理,如果我在性別和/或位置填充特種一起,查詢之前給我任何東西回來的唯一時間。

我該如何解決?

+0

你的意思是你的性別/位置在你的數據庫中是空白的?當你輸入性別和/或位置時,你會得到多少結果? –

+0

背後的想法是,如果我留下任何變量空白,它會搜索所有? – Si8

回答

1

如果性別丟失,可能是因爲缺失,所以沒有<gender>分隔符。也許這將解決這個問題:

(@strGender = '' or (content_html LIKE '%<gender>'+ @strGender+'</gender>%')) . . . 
+0

我在想如果我離開'Gender ='''那麼這是一個通配符查詢? – Si8

+0

無論「@ strGender」的值如何,您的原始查詢都在尋找與性別分隔符匹配的內容。 –

+0

我更新了我的問題,使其更清晰一些。如果用戶爲他們中的任何一個選擇ALL,那麼它應該搜索該類別中的ALL? – Si8