C# Mysql multiple queries

C4d picture C4d · Sep 4, 2014 · Viewed 15k times · Source

Im trying to build up a little status-tool. I need to get results of multiple queries (about 4-5). The general connection-setup and 'how-to-read-data' is already done but I cant figure out how the another query executed.

Everything I found while searching for it is for the SqlClient. Im totally overcharged with this.

Here is my code so far (be patient, im a newbie to this):

private void button1_Click(object sender, EventArgs e)
        {
            if(listView1.Items.Count > 1)
            {
                listView1.Items.Clear();
            }
            var listMember = new List<string>{};
            var listOnline = new List<string>{};

            // SQL PART //
            string connString = "Server=10*****;Port=3306;Database=e***;Uid=e***;password=********************;";
            MySqlConnection conn = new MySqlConnection(connString);
            MySqlCommand command = conn.CreateCommand();
            command.CommandText = "SELECT fullname,online FROM member WHERE active = '1' ORDER BY online DESC";
            try
            {
                conn.Open();
            }
            catch (Exception ex)
            {
                listView1.Items.Add("Error: " + ex);
            }
            MySqlDataReader reader = command.ExecuteReader();
            while(reader.Read())
            {
                listMember.Add(reader["fullname"].ToString());
                listOnline.Add(reader["online"].ToString());
            }
            conn.Close();
            // SQL ENDING //

            // SET ENTRIES TO LISTVIEW //
            int counter = 0;
            foreach(string member in listMember)
            {
                ListViewItem item = new ListViewItem(new[] { member, listOnline.ElementAt(counter) });
                item.ForeColor = Color.Green;
                listView1.Items.Add(item);

                counter++;
            }
        }

Im not really sure how the design/layout will look like in the end, so I would like to just append the results to lists in the sql-part to process the data later out of the lists.

Do I really have to setup a complete new connection after conn.Close()? Or is there any other way? I can just imagine: 5 queries with their own connection,try,catch and 2 loops... this will get about 100-200 lines just for getting the results out of 5 queries. Isnt that a bit too much for such an easy thing?

Hope for some help. Greetings.

According to the new comments my latest code:

Top:

public partial class Form1 : Form
{
    public static string connString = "Server=10****;Port=3306;Database=e****;Uid=e****;password=****;";
    public Form1()
    {
        InitializeComponent();
        MySqlConnection conn = new MySqlConnection(connString); // Error gone!
    }

Body part:

 public void QueryTwoFields(string s, List<string> S1, List<string> S2)
        {
            try
            {
                MySqlCommand cmd = conn.CreateCommand(); // ERROR: conn does not exist in the current context.
                cmd.CommandType = CommandType.Text;
                string command = s;
                cmd.CommandText = command;
                MySqlDataReader sqlreader = cmd.ExecuteReader();
                while (sqlreader.Read())
                {
                    S1.Add(sqlreader[0].ToString());
                    S2.Add(sqlreader[1].ToString());
                }
                sqlreader.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

private void button1_Click(object sender, EventArgs e)
        {
            if(listView1.Items.Count > 1)
            {
                listView1.Items.Clear();
            }
            var listMember = new List<string>{};
            var listOnline = new List<string>{};

            using (conn) // ERROR: conn does not exist in the current context.
            {
            conn.Open();
            ///...1st Query
            QueryTwoFields("SELECT fullname,online FROM member WHERE active = '1' ORDER BY online DESC",listMember,listOnline);
            //...2nd query
            //QueryTwoFields("your new Select Statement", otherList, otherList); 
            }
        }

Answer

apomene picture apomene · Sep 4, 2014

You don't have to close connection every time you execute one query rarher than close the sqlreader assigned to that connection. Finally when all of your queries have been executed you close the connection. Consider also the use of using:

You cal also define a method for execution your Query in order for your code not to be repetive:

    public  void QueryTwoFields(string s, List<string> S1, List<string> S2)    
///Select into List S1 and List S2 from  Database (2 fields)
              {
                  try
                  {             
                          MySqlCommand cmd = conn.CreateCommand();
                          cmd.CommandType = CommandType.Text;
                          string command = s;
                          cmd.CommandText = command;
                          MySqlDataReader sqlreader = cmd.ExecuteReader();
                          while (sqlreader.Read())
                          {
                              S1.Add(sqlreader[0].ToString());
                              S2.Add(sqlreader[1].ToString());            
                          }
                          sqlreader.Close();

                  }
                  catch (Exception ex)
                  {                          
                     MessageBox.Show(ex.ToString());            
                  }                                           
              }
private void button1_Click(object sender, EventArgs e)
        {
            if(listView1.Items.Count > 1)
            {
                listView1.Items.Clear();
            }
            var listMember = new List<string>{};
            var listOnline = new List<string>{};

            // SQL PART //

    using (conn)
    {
       conn.Open();
       ///...1st Query
       QueryTwoFields("SELECT fullname,online FROM member WHERE active = '1' ORDER BY online DESC",listmember,listonline)
        //...2nd query
        QueryTwoFields("your new Select Statement",myOtherList1,myOtherlist2)    
      ....
    }
}

EDIT : Take in mind you cant define QueryTwoFields method inside button handler. You must define it outside (see code above). Also Define your connection data in the start of the programm:

    namespace MyProject
    {
        /// <summary>
        /// Defiine your connectionstring and connection
        /// </summary>
        /// 

        public partial class Form1 : Form
        {  public static string connString = "Server=10*****;Port=3306;Database=e***;Uid=e***;password=********************;";              
           MySqlConnection  conn = new MySqlConnection(connString);

.........