我有一个设置表单的循环,它是以下代码(在表单加载事件中)。这将显示一个带有人员姓名的复选框。如果位字段为 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");
}
}
}