Pregunta

I have a chart that linked to MySQL database. I want to update the chart every second. I use the code below to load the Database to my chart :

private void button4_Click(object sender, EventArgs e)
    {
        MySqlConnection conDataBase = new MySqlConnection(constring);
        MySqlCommand cmdDataBase = new MySqlCommand(" select * from konsentrasi.okedeh ;", conDataBase);
        MySqlDataReader myReader;
        try
        {
            conDataBase.Open();
            myReader = cmdDataBase.ExecuteReader();

            while (myReader.Read())
            {
                chart1.Series["konsentrasi"].Points.AddXY(myReader.GetInt32("nomor"), myReader.GetInt32("konsentrasi"));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

Then I use timer to update the chart :

chart1.Update();

But nothing in the chart changed. So, what should I do to update the chart? And how I can scroll the chart to load just some data. For example it only show 10 data?

¿Fue útil?

Solución

You should add an ORDER clause to your SQL (probably DESC for some timestamp or ID) and a LIMIT clause to get only the last 10 records. Something like that:

SELECT * from konsentrasi.okedeh ORDER BY id DESC LIMIT 10; 

I hope it works for you.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top