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

时间:2015-04-16 07:07:01

标签: sql checkbox datagridview sql-update

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

当我运行教师页面时,它会阻止已被管理员阻止的特定复选框,然后当我再次从教师页面提交时,它会使用test.db中的新值更新所有旧值,并将其解锁为其他院系也是。

我真正需要的是当管理员阻止复选框时,应该始终阻止所有其他教员,当教师也选择其他复选框并提交时,所有其他教师也应该阻止该复选框。

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

 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语句。建议我怎么做。

1 个答案:

答案 0 :(得分:2)

您可以在以下内容中使用CASE Expression:

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