Click here to Skip to main content
15,914,642 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I would like to use string[] to query in a loop.

my idea is: keep my information in string[] and place in query string,specify with index

here is my code
C#
string[] edit_list = txtReqList.Text.Split('\n');
string query ;
MySqlCommand cmd = new MySqlCommand();
for (int i = 0; i < edit_list.Length; i++)
{
    try
    {
        query = "UPDATE submit
                 SET submit_status ='S'
                 WHERE submit_id = '" + edit_list[i] + "'";
        //Open connection
        if (this.OpenConnection() == true)
        {
            //Assign the query using CommandText
            cmd.CommandText = query;
            //Assign the connection using Connection
            cmd.Connection = connection;
            //Execute query
            cmd.ExecuteNonQuery();

        }
    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.ToString());
    }
    this.CloseConnection();
}


Unfortunately, the only last index is queried, e.g.

my string[] are : 0,1,2,3.

following from my idea, it should update at submit_id = 0,1,2 and 3
but, it update only submit_id = 3.

how can this be? and how can I slove this problem?

Thank you for your answer.

PS. I use MVSC# as tool, in Windows Forms Application Mode
Posted

Instead of running that in a loop, why not issue just the one command? SQL has an IN clause:
SQL
UPDATE submit SET submit_status ='S' WHERE submit_id IN (list)
All you have to do is build the list:
C#
StringBuilder sb = new StringBuilder();
sb.Append("UPDATE submit SET submit_status ='S' WHERE submit_id IN (");
sb.Append(string.Join(",", edit_list));
sb.Append(")");
cmd.CommandText = sb.ToString();
 
Share this answer
 
Comments
Vani Kulkarni 18-Jun-12 6:51am    
Clear and crisp.
windyl3ig 19-Jun-12 2:26am    
oh yes, nice and clear, very thank you for your guideline. ^^
OriginalGriff 19-Jun-12 2:36am    
You're welcome!
Hello can you try with replacing this line on top most of your code.

C#
string[] edit_list = txtReqList.Text.Split('\n');
with 
string[] edit_list = txtReqList.Text.Split(',');


now debug your code and check what will be there in edit_list items.
please provide that result if this is not re-solved yet.
 
Share this answer
 
v2
1.) Split should be on comma and not on line-break?
2.) Should the connection be closed after the foreach loop?

C#
string[] edit_list = txtReqList.Text.Split('\,');
string query ;
MySqlCommand cmd = new MySqlCommand();
for (int i = 0; i < edit_list.Length; i++)
{
    try
    {
        query = String.Format("UPDATE submit SET submit_status='S' WHERE submit_id = '{0}'", edit_list[i]);

        //Open connection
        if (this.OpenConnection() == true)
        {
            //Assign the query using CommandText
            cmd.CommandText = query;
            //Assign the connection using Connection
            cmd.Connection = connection;
            //Execute query
            cmd.ExecuteNonQuery();
        }
    }
    catch (MySqlException ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

this.CloseConnection();
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900