在SQL中更新多个数据条目

时间:2016-01-05 15:18:25

标签: c# sql-server loops sql-update checkboxlist

我得到一个日期时间列表,并将它们存储在一个带有这段代码的复选框列表中:

    <anthem:CheckBox ID="chkAll" runat="server" OnCheckedchanged="chkAll_CheckedChanged" Text="Select/Deselect All" AutoPostBack="true"
        style="margin-left: 128px" >
    </anthem:CheckBox>
    <anthem:CheckBoxList ID="CheckOpenTimesheets"  runat="server" OnSelectedIndexChanged="checkbox_Selected" AutoPostBack="true"
        style="margin-left: 128px" >
    </anthem:CheckBoxList>

以下是相关代码:

            List<ListItem> toBeRemoved = new List<ListItem>();

            for (int i = 1; i < CheckOpenTimesheets.Items.Count; i++)
            {
                toBeRemoved.Add(CheckOpenTimesheets.Items[i]);
            }
            for (int i = 0; i < toBeRemoved.Count; i++)
            {
                CheckOpenTimesheets.Items.Remove(toBeRemoved[i]);
            }

            String sql = "SELECT StartDate FROM Periods WHERE User_ID = @userid AND (PeriodStatus_ID = 1 OR PeriodStatus_ID = 2) ORDER BY StartDate DESC";
            command.CommandText = sql;
            command.Parameters.Add(new SqlParameter("userid", ddlActingAs.SelectedValue.ToString()));

            SqlDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {

                ListItem item = new ListItem();
                item.Text += reader.GetDateTime(0).ToString("MM/dd/yyyy") + " is open";
                item.Value = reader["StartDate"].ToString();

                CheckOpenTimesheets.Items.Add(item);

            }
            CheckOpenTimesheets.UpdateAfterCallBack = true;

            reader.Close();

//The functions below are for selecting/deselecting the items in the checklistbox
protected void chkAll_CheckedChanged(object sender, EventArgs e)
{
    foreach (ListItem item in CheckOpenTimesheets.Items)
    {
        item.Selected = chkAll.Checked;
    }
}

protected void checkbox_Selected(object sender, EventArgs e)
{
    chkAll.CheckedChanged -= chkAll_CheckedChanged;

    CheckBoxList checkOpenTimesheets = (CheckBoxList)sender;
    if (allItemsCheckedInCheckBoxList(checkOpenTimesheets))
    {
        chkAll.Checked = true;
    }
    else if (allItemsUnCheckedInCheckBoxList(checkOpenTimesheets))
    {
        chkAll.Checked = false;
    }

    chkAll.CheckedChanged += chkAll_CheckedChanged;
}

private bool allItemsCheckedInCheckBoxList(CheckBoxList checkBoxList)
{
    bool allItemsChecked = true;

    foreach (ListItem item in checkBoxList.Items)
    {
        allItemsChecked = item.Selected;

        if (!allItemsChecked)
            break;
    }

    return allItemsChecked;
}

private bool allItemsUnCheckedInCheckBoxList(CheckBoxList checkBoxList)
{
    bool allItemsUnChecked = false;

    foreach (ListItem item in checkBoxList.Items)
    {
        allItemsUnChecked = item.Selected;

        if (allItemsUnChecked)
            break;
    }

    return allItemsUnChecked;
}

我要做的是,当我点击页面上的按钮时,它会遍历复选框列表,并且每个被检查的项目都会将PeriodStatus_ID更新为5中的每个项目。 SQL表。

点击按钮的功能在这里(不知道该怎么做):

protected void SubmitAll_Click(object sender, EventArgs e)
{
    foreach (ListItem item in CheckOpenTimesheets.Items)
    {
        SqlCommand command = new SqlCommand();
        command.Connection = gConn;
        if (item.Selected == true)
        {
            String sql = "UPDATE Periods SET PeriodStatus_ID=5 WHERE User_ID = @userid AND StartDate = @startdate";
            command.CommandText = sql;
            command.Parameters.Add(new SqlParameter("userid", ddlActingAs.SelectedValue.ToString()));
            command.Parameters.Add(new SqlParameter("startdate", item.Value));
        }
    }
}

任何有关实现这一目标的想法都会很棒,感谢提前。

1 个答案:

答案 0 :(得分:1)

您更新了代码,因此我更新了我的代码,但我改变了主意,想要如何构建代码。您还遗漏了ExecuteNonQuery语句,the way发送Update命令。

protected void SubmitAll_Click(object sender, EventArgs e)
{

     SqlCommand command = new SqlCommand();
     command.Connection = gConn;
     String sql = "UPDATE Periods SET PeriodStatus_ID=5 WHERE User_ID = @userid AND StartDate = @startdate";
     command.CommandText = sql;
     command.Parameters.Add("userid");
     command.Parameters.Add("startdate");

    for (int i = 0; i < CheckOpenTimesheets.Items.Count; i++)
    {

     if (item.Selected == true)
     {
        command.Parameters("userid").Value = ddlActingAs.SelectedValue.ToString();
        command.Parameters("startdate").Value = item.Value;

        command.ExecuteNonQuery();
        }
    }
}