我正在使用MySQL的数据创建一个实时图表,但我找不到办法。
float Sen3, Sen2, Sen5;
string Sen3Value, Sen2Value, Sen5Value;
//open connection to database
private void ShowData(object sender, EventArgs e)
{
MySqlConnection connection = new MySqlConnection(myConnectionString);
connection.Open();
try
{
MySqlCommand cmd = connection.CreateCommand();
cmd.CommandText = "Select * FROM sensor";
MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
dataGridView1.DataSource = ds.Tables[0].DefaultView;
///////////////////////////////////////////////////////
this.dataGridView1.Sort(this.dataGridView1.Columns["id"], ListSortDirection.Descending);
//////////////////////////////////////////////////////
int nRowIndex = dataGridView1.Rows.Count;
int first = dataGridView1.Rows.Count - nRowIndex;
dataGridView1.Rows[first].Selected = true;
if (dataGridView1.Rows[first].Cells[2].Value != null)
{
Sen3Value = dataGridView1.Rows[first].Cells[3].Value.ToString();
Sen3 = float.Parse(Sen3Value);
}
if (dataGridView1.Rows[first].Cells[2].Value != null)
{
Sen2Value = dataGridView1.Rows[first].Cells[4].Value.ToString();
Sen2 = float.Parse(Sen2Value);
}
if (dataGridView1.Rows[first].Cells[2].Value != null)
{
Sen5Value = dataGridView1.Rows[first].Cells[5].Value.ToString();
Sen5 = float.Parse(Sen5Value);
}
}
catch (Exception)
{
throw;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Clone();
}
}
}
// Create a new chart.
ChartControl ManhattanBarChart = new ChartControl();
// Add a bar series to it.
DevExpress.XtraCharts.Series series1 = new DevExpress.XtraCharts.Series("Series 1", ViewType.ManhattanBar);
DevExpress.XtraCharts.Series series2 = new DevExpress.XtraCharts.Series("Series 2", ViewType.ManhattanBar);
// Add points to the series.
series1.Points.Clear();
series2.Points.Clear();
series1.Points.Add(new SeriesPoint("A", Sen2));
series1.Points.Add(new SeriesPoint("B", 8));
series1.Points.Add(new SeriesPoint("C", 9));
series1.Points.Add(new SeriesPoint("D", 10));
series2.Points.Add(new SeriesPoint("A", 3));
series2.Points.Add(new SeriesPoint("B", 4));
series2.Points.Add(new SeriesPoint("C", 5));
series2.Points.Add(new SeriesPoint("D", Sen2));
如代码所示,Sen2是来自MySQL的数据,但在运行代码时它显示为0。任何帮助将不胜感激!
编辑:添加"打开与数据库的连接"码
请原谅我缺少C#编程,因为这是我的第一个需要此项目的项目