I'm going to use the dataReader to bind data to C# and chart controls.But currently the table does not display data. There is no display on the Chart control My solution and tried method is as follows: show it on chart control, x axis is time (database variable is Time1), y axis is ranking (database variable is ranking)
private void TmainForm_Load(object sender, EventArgs e) {
string strconn;
//Initializes the connection string
strconn = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;Connect Timeout=30";
string strSQL; //The query command
DataSet ds1 = new DataSet(); //dataset1:ds1
//strSQL = "select * from Grade where Sname =N'" + textBox1.Text + "'";
strSQL = "select ranking,Time1 from Grade ";
SqlConnection conn = new SqlConnection(strconn); //Connection object
conn.Open();
SqlDataAdapter myDa = new SqlDataAdapter(strSQL, conn); //DataAdapter
SqlCommand comm = new SqlCommand(); //SqlCommand
comm.Connection = conn;
comm.CommandText = strSQL;
comm.CommandType = CommandType.Text;
SqlDataReader dr;
dr = comm.ExecuteReader();
dataGridView1.DataSource = dr;
try
{
conn.Open();
myDa.Fill(ds1, "ranking");
Series dataTable3Series = new Series("dataTable3"); //dataTable3 is the datasource
dataTable3Series.Points.DataBind(dr, "Time1", "ranking", "");
dataTable3Series.XValueType = ChartValueType.DateTime; //Set the Time1 to X-axis
dataTable3Series.ChartType = SeriesChartType.Line; //Set the ranking to Y-axis
chart1.Series.Add(dataTable3Series); //add to chart1
** here is the data binding **
myDa.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
this.Tag = false;
}
finally
{
conn.Close(); // close connection
}
dr.Close();