问题
I have a loop that sets up the form, which is the following code (in the form load event). This displays a checkbox with the persons name. It checks the checkbox if a bit field is 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;
}
Later on in the code (for a save button click event), it runs the same select but also a load of updates to set the InOperation field to true/false depending on the tick. It also resets the OperationOrder if you are being added to the Operation.
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");
}
}
The problem with this is that it updates every single row based upon the last object created (e.g. if 5 rows, only the bottom checkbox would count for running the SQL, and it applies to all of them). How can I update every single row, is there a way I can reference each object I create rather than just the last one created?
UPDATE: Here is some of the new code that is causing errors. public partial class SelectUsers : Form { public int userID; public List myBoxes;
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");
}
}
}
回答1:
You should keep an array of checkboxes rather than just an individual checkbox
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);
}
And later ir your for loop:
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");
}
}
That should do it.
It goes without saying, that executing SQL statements directy in your form is not a good idea, but thats another story
回答2:
Wouldn't it be A LOT faster if you could simply loop over all the checkboxes on your form, build a SQL update script from there and do a .runUpdate() of said script in the end ?
Things that come to mind :
- make sure to use a stringBuilder
- first part update the InOperation field only
- then update the OperationOrder in 1 single UPDATE
You'll need a way to link the checkbox with the relevant UserId off course.
回答3:
Carlos Grappa answer was pointing me in the right direction by saying use an array, but it was setup wrong. After reading through programming books, I found that you create an array as follows:
// 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 you are probably right, however I do not know how to do this.
来源:https://stackoverflow.com/questions/14760263/reference-previous-object-created-in-loop-to-update-sql