我有一個情況,其中參數我的sql查詢將dynamic.if參數爲空我不想將其添加到查詢,我已經嘗試了一些事情(從未工作過)..它現在看起來像stoopid我建立動態sql查詢之間和按關鍵字排序
ds = SqlHelper.ExecuteDataset(GlobalSettings.DbDSN, CommandType.Text, "SELECT TOP 1000 [ID],[Project],[Owner],[Consultant],[Contractor],[Value],[Level1],[Level2] ,[Status] ,[Category] ,[Country],[CreatedDate],[CreatedByID],[CreatedByName] FROM [tbl_Projects] where"+if(!string.IsNullOrEmpty(paraCategory)){ "[Category] = @Category and"}+"+ Country [email protected] and "+if(!string.IsNullOrEmpty(paraCategory)){ " value between @val1 and @val2"}+" order by CreatedDate asc",
new SqlParameter("@Category", paraCategory),
new SqlParameter("@Country", paraCountry),
new SqlParameter("@val1", paraValue1),
new SqlParameter("@val2", paraValue2));
我已經檢查了構建動態SQL還 here 但它不是有用的,我需要把像和words..can任意一個鍵之間幫我一下嗎?
這看起來像C#給我。你能否相應地標記你的問題? –
@FabianBigler是的,我已經添加了一些C#代碼到我的SQL查詢來處理這..這是愚蠢的..現在我試圖通過SQL本身解決這..這是這篇文章的原因。我也編輯了標籤正如你所說的..如果解決方案只用C#我不想錯過它 – Athul
行。我希望我的回答能幫助你。其實很簡單。 –