【问题标题】:Iterating if loop based on the result or conditions of the sql query根据sql查询的结果或条件迭代if循环
【发布时间】:2014-04-30 19:21:18
【问题描述】:

好的,这是我的代码

protected void TextBox1_TextChanged(object sender, EventArgs e)
{

    if (bll.MainEnq_Stu_Name(en) != null)
    {
        Label9.Text = bll.MainEnq_Stu_Name(en).ToString();
    }
    else
    {
        Label9.Text = "No Records Found!";
    }  
}

这里是业务逻辑层的查询。

  //MainEnquiry.aspx panel1
    public object MainEnq_Stu_Name(EntityLayer.Entity en)
    {
        return dll.GetSingleValue("select name from studentinfo where mobile=" + en.mainEnq_Stu_Mobile+"and dob is not null");
    }

现在我想做的是在上面的代码中为 if 迭代添加一个更多功能。

that is if mobile is null print "no records found".
and if dob is not null print "this user already exists"

【问题讨论】:

    标签: c# asp.net sql-server if-statement 3-tier


    【解决方案1】:

    您需要包括在您的案例中所需的列,它的名称、移动设备和出生日期。 所以先这样做

    //MainEnquiry.aspx panel1
    public object MainEnq_Stu_Name(EntityLayer.Entity en)
    {
        return dll.GetSingleValue("select name,mobile,dob from studentinfo where mobile=" + en.mainEnq_Stu_Mobile+"and dob is not null");
    }
    

    然后只是 else if 您的代码中的条件。希望你能从这里出发。

    【讨论】:

    • 虽然我必须对以前的代码进行很多更改,但是是的,您的代码有效......不过非常感谢您:)
    【解决方案2】:

    您可以在 SQL 选择语句中使用 case 语句。但我不知道以下是否是您想要做的。您要返回三列还是只返回一列?

        //MainEnquiry.aspx panel1
        public object MainEnq_Stu_Name(EntityLayer.Entity en)
        {
            return dll.GetSingleValue("select case when mobile is null then 'no records found' when dob is null then 'this user already exists' else name end from studentinfo where mobile=" + en.mainEnq_Stu_Mobile+"and dob is not null");
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-18
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 2018-07-16
      • 2020-08-28
      相关资源
      最近更新 更多