如何在更新查询中使用if语句?

How to use if statement in update query?

我想在 selected 时阻止复选框。我有 adminfaculty 页面,当管理员阻止特定复选框时,它应该阻止所有院系的特定复选框。这些复选框在网格中。每个教员都有单独的登录名,但所有教员都有相同的网格。当一个教员阻止特定复选框时,其他教员也应该被阻止。

当我 运行 教师页面时,它会阻止已被管理员阻止的特定复选框,然后当我再次从教师页面提交时,它会用 [=12= 中的新值更新所有旧值] 并且它也对其他院系畅通无阻。

我真正需要的是,当管理员阻止该复选框时,它应该始终为所有其他教员阻止,当教员还 select 其他一些复选框并提交该特定复选框时,也应该为所有其他教员阻止.

这是我用于更新的代码。但它不起作用

 admin:

 protected void Button2_Click(object sender, EventArgs e)
    {
        using (SqlConnection con2 = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\users\work station\documents\visual studio 2012\Projects\NewTimeTable\NewTimeTable\App_Data\Admindetail.mdf;Integrated Security=True"))
          {
            string datedif = r.Cells[0].Text;
                    string dayweek = ddlclass.SelectedValue + ' ' + ddlmonth.SelectedValue + ' ' + ddlcourse.SelectedValue + ' ' + ddlsemester.SelectedValue + ' ' + ddlweek.SelectedValue;
                    con2.Open();
                    SqlCommand comm = new SqlCommand("insert into test (datedif,hour1,hour2,hour3,hour4,hour5,dayweek) values (@datedif,@hour1,@hour2,@hour3,@hour4,@hour5,@dayweek) ", con2);
                    comm.Parameters.AddWithValue("@datedif", datedif);
                    comm.Parameters.AddWithValue("@hour1", hour1);
                    comm.Parameters.AddWithValue("@hour2", hour2);
                    comm.Parameters.AddWithValue("@hour3", hour3);
                    comm.Parameters.AddWithValue("@hour4", hour4);
                    comm.Parameters.AddWithValue("@hour5", hour5);
                    comm.Parameters.AddWithValue("@dayweek", dayweek);
                    comm.ExecuteNonQuery();
                    con2.Close();
                }
            }

教师:

protected void btnsubmit_Click(object sender, EventArgs e)
    {
        using (SqlConnection con1 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                        string datedif = r.Cells[0].Text;
                        string subject1 = ddlsubjects.SelectedValue;
                        string subject2 = ddlsubjects.SelectedValue;
                        string subject3 = ddlsubjects.SelectedValue;
                        string subject4 = ddlsubjects.SelectedValue;
                        string subject5 = ddlsubjects.SelectedValue;
                        con2.Open();
                        SqlCommand comm = new SqlCommand("UPDATE test SET hour1 = @hour1,subject1 = @subject1,hour2 = @hour2,subject2 = @subject2,hour3 = @hour3,subject3 = @subject3,hour4 = @hour4,subject4 = @subject4,hour5 = @hour5,subject5 = @subject5 where datedif =@datedif and dayweek='"+ddlweek.SelectedValue+"'", con2);
                        comm.Parameters.AddWithValue("@hour1", hour1);
                        comm.Parameters.AddWithValue("@datedif", datedif);
                        comm.Parameters.AddWithValue("@subject1", subject1);
                        comm.Parameters.AddWithValue("@hour2", hour2);
                        comm.Parameters.AddWithValue("@subject2", subject2);
                        comm.Parameters.AddWithValue("@hour3", hour3);
                        comm.Parameters.AddWithValue("@subject3", subject3);
                        comm.Parameters.AddWithValue("@hour4", hour4);
                        comm.Parameters.AddWithValue("@subject4", subject4);
                        comm.Parameters.AddWithValue("@hour5", hour5);
                        comm.Parameters.AddWithValue("@subject5", subject5);
                        comm.ExecuteNonQuery();
                        con2.Close();
                    }
                }

我想在该更新查询中使用 If 语句。建议我怎么做。

您可以在以下情况下使用 CASE 表达式:

UPDATE test
SET
    Hour = CASE WHEN LEN(@Hour) > 0 THEN @Hour ELSE @Hour1 END,
    Password = CASE WHEN LEN(@Password) > 0 THEN @Password ELSE Password END
    ....
WHERE
    ....

编辑

CASE WHEN 'True' THEN @TrueCondition ELSE @FalseCondition END