查詢不工作,我有我的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如下:
的All Locations
,All Specialties
,Any Gender
有""
這使得應該將下面的代碼搜索所有,如果我理解正確的價值?
(content_html LIKE '%<gender>'+ @strGender+'</gender>%')
AND
(content_html LIKE '%'[email protected]+'%')
AND
(content_html LIKE '%'[email protected]+'%')
它的工作原理,如果我在性別和/或位置填充特種一起,查詢之前給我任何東西回來的唯一時間。
我該如何解決?
你的意思是你的性別/位置在你的數據庫中是空白的?當你輸入性別和/或位置時,你會得到多少結果? –
背後的想法是,如果我留下任何變量空白,它會搜索所有? – Si8