0

我有一个设置表单的循环,它是以下代码(在表单加载事件中)。这将显示一个带有人员姓名的复选框。如果位字段为 1,它会检查复选框。

int xAxisCheckbox = 130;
int yAxisCheckbox = 30;

for (int i = 0; i < selectDS.Tables[0].Rows.Count; i++)
{
    this.myCheckBox = new CheckBox();
    myCheckBox.Location = new Point(xAxisCheckbox, yAxisCheckbox);
    myCheckBox.Size = new Size(120, 20);
    myCheckBox.Text = selectDS.Tables[0].Rows[i]["FullName"].ToString();
    myCheckBox.Checked = (bool)selectDS.Tables[0].Rows[i]["InOperation"];
    yAxisCheckbox = yAxisCheckbox + 80;
}

稍后在代码中(对于保存按钮单击事件),它运行相同的选择,但也运行大量更新以根据刻度将 InOperation 字段设置为 true/false。如果您被添加到操作中,它还会重置操作顺序。

for (int i = 0; i < selectDataSet.Tables[0].Rows.Count; i++)
{
     userID = (int)selectDataSet.Tables[0].Rows[i]["UserID"];
     if (myCheckBox.Checked)
     {
         connection.runUpdate("UPDATE Users SET InOperation = 1, OperationOrder = CASE WHEN OperationOrder = 1 THEN 1 ELSE CASE WHEN InOperation=1 THEN OperationOrder ELSE (SELECT COUNT(*)+1 FROM Users WHERE InOperation=1 AND OperationOrder > 0) END END WHERE UserID=" + userID);
         connection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder END WHERE InOperation=1");
      }
      else
      {
          connection.runUpdate("UPDATE Users SET InOperation = 0, OperationOrder = 0 WHERE UserID=" + userID);
          connection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder -1 = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder -1 END WHERE InOperation=1");
       }
   }

这样做的问题是它会根据创建的最后一个对象更新每一行(例如,如果有 5 行,则只有底部的复选框会计算运行 SQL,并且它适用于所有这些)。如何更新每一行,有没有一种方法可以引用我创建的每个对象,而不仅仅是创建的最后一个对象?

更新:这是一些导致错误的新代码。公共部分类 SelectUsers : Form { public int userID; 公开列出我的盒子;

    public SelectUsers()
    {
        InitializeComponent();
    }

    private void SelectUsers_Load(object sender, EventArgs e)
    {
        DataSet ds = myconnection.runSelect(new DataSet(), "THE SELECT");

        int xAxisCheckbox = 40;
        int yAxisCheckbox = 50;

        myBoxes = new List<CheckBox>();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            this.myCheckBox = new CheckBox();
            myCheckBox.Location = new Point(xAxisCheckbox, yAxisCheckbox);
            myCheckBox.Size = new Size(120, 20);
            myCheckBox.Text = ds.Tables[0].Rows[i]["FullName"].ToString();
            myCheckBox.Checked = (bool)ds.Tables[0].Rows[i]["InOperation"];
            yAxisCheckbox = yAxisCheckbox + 80;
            myBoxes.Add(myCheckBox);
        }
    }

    private void saveBtn_Click(object sender, EventArgs e)
    {
        DataSet ds = myconnection.runSelect(new DataSet(), "THE SELECT");

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            userID = (int)ds.Tables[0].Rows[i]["UserID"];
            if (myBoxes[i].Checked)
            {
                myconnection.runUpdate("UPDATE Users SET InOperation = 1, OperationOrder = CASE WHEN OperationOrder = 1 THEN 1 ELSE CASE WHEN InOperation=1 THEN OperationOrder ELSE (SELECT COUNT(*)+1 FROM Users WHERE InOperation=1 AND OperationOrder > 0) END END WHERE UserID=" + userID);
                myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder END WHERE InOperation=1");
            }
            else
            {
                myconnection.runUpdate("UPDATE Users SET InOperation = 0, OperationOrder = 0 WHERE UserID=" + userID);
                myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder -1 = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder -1 END WHERE InOperation=1");
            }
        }
    }
4

3 回答 3

1

您应该保留一系列复选框,而不仅仅是单个复选框

int xAxisCheckbox = 130;
int yAxisCheckbox = 30;

List<CheckBox> myBoxes = new List<CheckBox>();

for (int i = 0; i < selectDS.Tables[0].Rows.Count; i++)
{
    this.myCheckBox = new CheckBox();
    myCheckBox.Location = new Point(xAxisCheckbox, yAxisCheckbox);
    myCheckBox.Size = new Size(120, 20);
    myCheckBox.Text = selectDS.Tables[0].Rows[i]["FullName"].ToString();
    myCheckBox.Checked = (bool)selectDS.Tables[0].Rows[i]["InOperation"];
    yAxisCheckbox = yAxisCheckbox + 80;
    myBoxes.Add(myCheckBox);
}

后来你的for循环:

for (int i = 0; i < selectDataSet.Tables[0].Rows.Count; i++)
{
     userID = (int)selectDataSet.Tables[0].Rows[i]["UserID"];
     if (myBoxes[i].Checked)
     {
         connection.runUpdate("UPDATE Users SET InOperation = 1, OperationOrder = CASE WHEN OperationOrder = 1 THEN 1 ELSE CASE WHEN InOperation=1 THEN OperationOrder ELSE (SELECT COUNT(*)+1 FROM Users WHERE InOperation=1 AND OperationOrder > 0) END END WHERE UserID=" + userID);
         connection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder END WHERE InOperation=1");
      }
      else
      {
          connection.runUpdate("UPDATE Users SET InOperation = 0, OperationOrder = 0 WHERE UserID=" + userID);
          connection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder -1 = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder -1 END WHERE InOperation=1");
       }
   }

那应该这样做。

不用说,直接在你的表单中执行 SQL 语句不是一个好主意,但那是另一回事

于 2013-02-07T20:31:41.090 回答
0

如果您可以简单地遍历表单上的所有复选框,从那里构建一个 SQL 更新脚本并在最后执行所述脚本的 .runUpdate() ,那会不会快很多?

想到的事情:

  • 确保使用 stringBuilder
  • 第一部分仅更新 InOperation 字段
  • 然后在 1 个单一的 UPDATE 中更新 OperationOrder

您需要一种方法将复选框与相关的 UserId 联系起来。

于 2013-02-07T20:37:03.350 回答
-1

Carlos Grappa 的回答是使用数组为我指明了正确的方向,但设置错误。在阅读了编程书籍后,我发现你创建了一个数组,如下所示:

// Global
CheckBox[] myCheckBox;
DataSet ds;

private void SelectUsers_Load(object sender, EventArgs e) {

    ds = myconnection.runSelect(new DataSet(), "THE SELECT");

    int xAxisCheckbox = 40;
    int yAxisCheckbox = 50;

    // Create the CheckBox array (amount based on number of rows)
    myBoxes = new CheckBox[ds.Tables[0].Rows.Count];

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        // Create a new Checkbox
        myCheckBox[i] = new CheckBox();

        myCheckBox.Location = new Point(xAxisCheckbox, yAxisCheckbox);
        myCheckBox.Size = new Size(120, 20);
        myCheckBox.Text = ds.Tables[0].Rows[i]["FullName"].ToString();
        myCheckBox.Checked = (bool)ds.Tables[0].Rows[i]["InOperation"];
        yAxisCheckbox = yAxisCheckbox + 80;

        // Add the Checkbox
        Controls.Add(myCheckBox[i]);
    }
}

private void saveBtn_Click(object sender, EventArgs e) {
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        userID = (int)ds.Tables[0].Rows[i]["UserID"];

        if (myBoxes[i].Checked)
        {
            myconnection.runUpdate("UPDATE Users SET InOperation = 1, OperationOrder = CASE WHEN OperationOrder = 1 THEN 1 ELSE CASE WHEN InOperation=1 THEN OperationOrder ELSE (SELECT COUNT(*)+1 FROM Users WHERE InOperation=1 AND OperationOrder > 0) END END WHERE UserID=" + userID);
            myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder END WHERE InOperation=1");
        }
        else
        {
            myconnection.runUpdate("UPDATE Users SET InOperation = 0, OperationOrder = 0 WHERE UserID=" + userID);
            myconnection.runUpdate("UPDATE Users SET OperationOrder = CASE WHEN OperationOrder -1 = 0 THEN (SELECT COUNT(*) FROM Users WHERE InOperation=1) ELSE OperationOrder -1 END WHERE InOperation=1");
        }
    }
}

Deroby 你可能是对的,但是我不知道该怎么做。

于 2013-03-22T17:12:34.077 回答