0
sc = new MySqlCommand("Update tbName set AvgTime=" + avgTimeArray[1]+ " where   PatternId=1", msc);
                sc.ExecuteNonQuery();

sc = new MySqlCommand("Update tbName set AvgTime=" + avgTimeArray[2]+ " where   PatternId=2", msc);
                sc.ExecuteNonQuery();

sc = new MySqlCommand("Update tbName set AvgTime=" + avgTimeArray[3]+ " where   PatternId=3", msc);
                sc.ExecuteNonQuery();

Is it possible to use a loop for this type of query set? If it is possble how to do it? (avgtImeArray is a array)

7
  • 7
    Yes, it's possible. Just use a loop. Do you have a specific question regarding what you've tried? Aug 25, 2015 at 13:08
  • 7
    1. Use loop. 2. Use using. 3. Use parameterized queries Aug 25, 2015 at 13:10
  • @TimSchmelter My problem is how to write this part (avgTimeArray[1]).... I can't just put i (variable for loop) instead of 1,2,3....
    – Sasa1234
    Aug 25, 2015 at 13:18
  • @Sasa1234: why can't you do that? Aug 25, 2015 at 13:24
  • @TimSchmelter because program takes it as i, not as a variable.
    – Sasa1234
    Aug 25, 2015 at 13:26

3 Answers 3

3
for(int i = 1; i < avgTimeArray.length; i++){
sc = new     MySqlCommand("Update tbName set AvgTime = @AvgTime where PatternId = @PatternID", msc);
sc.Parameters.Add(new ObjectParameter("AvgTime", avgTimeArray[i].ToString())); 
sc.Parameters.Add(new ObjectParameter("PatternID", i.ToString())); 
sc.ExecuteNonQuery();}

This will work around your potential SQL injection vulnerability. Note, code is untested. Answer based on Tinman7757's posted answer and tweaked as needed...

MSDN article on Parameterizing queries: https://msdn.microsoft.com/en-us/library/vstudio/Bb738521(v=VS.100).aspx. The problem with not parameterizing is it leaves the code open for someone with malicious intents to execute arbitrary SQL code (including but not limited to deletions and full blown table drops). You will want to obviously prevent this wherever possible.

4
  • Why patternID is larger than array index?
    – Ken Hung
    Aug 25, 2015 at 14:10
  • Arrays start with an index of 0 and go up from there. The original post shows pattern ID's starting at 1, so I made a presumption that pattern ID's will always start at 1, not zero. Aug 25, 2015 at 14:11
  • But the array index also starts from 1 in the original post.
    – Ken Hung
    Aug 25, 2015 at 14:17
  • 1
    Okay, code tweaked accordingly. To the Original poster, might be worth noting that this will actually skip over the first item in the array, if this is your intent, this code will work as is, if not, change the loop definition to i=0 and change PatternID to i+1.ToString when assigning to a parameter. Aug 25, 2015 at 14:22
0
for(int i = 0; i < avgTimeArray.length; i++){
sc = new     MySqlCommand("Update tbName set AvgTime=" +  
avgTimeArray[i]+ " where   PatternId=" + i, msc);
sc.ExecuteNonQuery();}

Like Tim Schmelter said use a loop. 'i' represents the index of the array comparable to arrayAtIndex[0],arrayAtIndex[1].instead put it in a loop with a using statment wrapped around it. and use arrayAtIndex[i]

If you need to start from a higher index use int i = 1

Hope this has some value.

0

Yes you can try something like:

  dim x as integer = 1
  Do
     dim MyAVG as Integer = avgTimeArray[x]
     sc = new MySqlCommand("Update tbName set AvgTime=" + MyAvg + " where PatternId=" + x, msc);
     sc.ExecuteNonQuery();
     x += 1
     if x > 3: exit do
  Loop
2
  • You have missed this part "avgTimeArray[3]" That is the problem I have faced.
    – Sasa1234
    Aug 25, 2015 at 13:24
  • @Sasa1234 So change the 3 to x.
    – D Stanley
    Aug 25, 2015 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.