Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 查询在两种情况下提取数据_C#_Sql_Sql Server - Fatal编程技术网

C# 查询在两种情况下提取数据

C# 查询在两种情况下提取数据,c#,sql,sql-server,C#,Sql,Sql Server,这段代码中的一切都很正常,我唯一的问题是,当我单击“today”from下拉列表时,DB中没有今天的数据,它仍然在拉数据,就好像它从“txtCity”中取值一样。只有当我把东西放在txtCity中,而不是我在DB中的东西,它才会返回空的 string strVal = hdnOption.Value; IFormatProvider provider = new System.Globalization.CultureInfo("en-GB", true);

这段代码中的一切都很正常,我唯一的问题是,当我单击“today”from下拉列表时,DB中没有今天的数据,它仍然在拉数据,就好像它从“txtCity”中取值一样。只有当我把东西放在txtCity中,而不是我在DB中的东西,它才会返回空的

        string strVal = hdnOption.Value;
        IFormatProvider provider = new System.Globalization.CultureInfo("en-GB", true);
        DateTime dtStart = new DateTime();
        DateTime? dtEnd = null;
        string strCity = null;

        if (strVal == "0")
        {
            HideCustomSearch();
            dtStart = DateTime.Today;
            dtEnd = DateTime.Today;

            if (txtCityName != null)
            {
                strCity = txtCityName.Text.ToString().Trim();
            }
        }
        if (strVal == "today") 
        {
            HideCustomSearch();
            dtStart = DateTime.Today;
            dtEnd = DateTime.Today;
            if (txtCityName != null)
            {
                strCity = txtCityName.Text.ToString().Trim();
            }
        }
        if (strVal == "weekly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddDays(-7).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "byweekly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddDays(-15).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "monthly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddMonths(-1).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "yearly")
        {
            HideCustomSearch();
            dtStart = DateTime.Now.AddYears(-1).Date;
            dtEnd = DateTime.Today;
            strCity = txtCityName.Text.ToString().Trim();
        }
        if (strVal == "custom")
        {
            ShowCustomSearch();
            //this.txtdtStart = "22/10/2010";
            //dtStart = DateTime.Parse("07-01-2013", provider,                System.Globalization.DateTimeStyles.NoCurrentDateDefault);
            string d = "07-01-2013";
            dtStart = DateTime.ParseExact(d, "dd-MM-yyyy", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
            dtEnd = DateTime.ParseExact(d, "dd-MM-yyyy", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
            strCity = txtCityName.Text.ToString().Trim();

            hdndtStart.Value = txtdtStart.ToString();
            hdndtEnd.Value = txtdtEnd.Text.ToString();
        }
        FillGridFilter(dtStart, dtEnd, strCity);
抱歉,我忘了添加查询

if @start_date = DAY(getdate()) and @end_date = null and @city = null
    begin
    select jp.id, city.name[City]
    , row_number() over (order by city.name) [sr_no]
    , count(jp.id) over (partition by name) as no_of_posts 
    , COUNT(od.id) over (partition by name) as no_of_employers
    ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

from rs_job_posting jp

inner join rs_job_posting_location jpl on jpl.id = jp.id
inner join rs_cor_city city on city.id = jpl.city_fk
inner join rs_organization_detail od on od.id = jp.id

where DAY(posting_date) = @start_date
order by no_of_posts Desc 
结束

试试这个

if (@start_date = DAY(getdate()) AND @end_date IS NULL AND @city IS NULL)
  begin
   select jp.id, city.name[City]
        , row_number() over (order by city.name) [sr_no]
        , count(jp.id) over (partition by name) as no_of_posts 
        , COUNT(od.id) over (partition by name) as no_of_employers
        ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = @start_date
    GROUP BY jp.id, city.name, ,CONVERT(varchar(12), jp.posting_date, 103)
    order by no_of_posts Desc 
 END
在Sql Server中,
NULL
是一个
未知值
,因此您确实无法将未知值与任何值进行比较,在Sql Server中检查NULL时,您应该使用
为NULL
为非NULL
,比较运算符(,=)不使用NULL值

if (@start_date = DAY(getdate()) AND @end_date IS NULL AND @city IS NULL)
  begin
   select jp.id, city.name[City]
        , row_number() over (order by city.name) [sr_no]
        , count(jp.id) over (partition by name) as no_of_posts 
        , COUNT(od.id) over (partition by name) as no_of_employers
        ,CONVERT(varchar(12), jp.posting_date, 103) [date_created]

    from rs_job_posting jp

    inner join rs_job_posting_location jpl on jpl.id = jp.id
    inner join rs_cor_city city on city.id = jpl.city_fk
    inner join rs_organization_detail od on od.id = jp.id

    where DAY(posting_date) = @start_date
    GROUP BY jp.id, city.name, ,CONVERT(varchar(12), jp.posting_date, 103)
    order by no_of_posts Desc 
 END

在Sql Server中,
NULL
是一个
未知值
,因此您确实无法将未知值与任何值进行比较,当在Sql Server中检查NULL时,您应该使用
为NULL
为非NULL
,比较运算符(,=)不使用NULL值

哪个查询正在提取数据。显示usSorry,但如果没有查询本身,几乎不可能知道出了什么问题。哪个查询正在提取数据。表示担忧,但如果没有查询本身,几乎不可能知道出了什么问题。它仍然是一样的…没有改变:(它仍然是一样的…没有改变:(