簡體   English   中英

如何檢查查詢字符串是否為空

[英]How to check if the query string is null

我正在使用C#查詢表並產生以下SQL行:

'det.aspx?ObjectID=' + CAST(CT.OBJECTID AS VARCHAR) + '&cid=45&fid=' + ISNULL(CAST(CT.A0900 AS VARCHAR), '') 'TD'

產生:

http://localhost:3652/Pages/det.aspx?ObjectID=1092648&cid=45&fid=

我試圖只獲取fid的值(如果它不為null或有值):

if (Request.QueryString["fid"] != null || Request.QueryString["fid"].Length > 0)
{
    string t = Request.QueryString["fid"];
}

使用上面的示例,它不應輸入if caluse,而應輸入。

我該如何解決。

將查詢更改為此:(在其中使用case語句)

'det.aspx?ObjectID=' + CAST(CT.OBJECTID AS VARCHAR) + '&cid=45'+

case when CT.A0900 is not null then '&fid='+CAST(CT.A0900 AS VARCHAR)
else '' end

檢查空字符串或空字符串時,請使用內置的.NET字符串方法string.IsNullOrEmptystring.IsNullOrWhiteSpace

if ( !string.IsNullOrWhiteSpace(Request.QueryString["fid"]) )
{
    string t = Request.QueryString["fid"];
}

當字符串由於or語句而為空時,此條件將成功。 請注意,empty與null不相同:

if (Request.QueryString["fid"] != null || Request.QueryString["fid"].Length > 0)
{
    // "fid" could be empty, not null which causes the first condition to succeed.
    string t = Request.QueryString["fid"];
}

您想在上面使用&&語句:

if (Request.QueryString["fid"] != null && Request.QueryString["fid"].Trim().Length > 0)
{
    string t = Request.QueryString["fid"];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM