private void Form1_Load(object sender, EventArgs e)
{
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
string str="Data Source=WIN-NVQAG2H5CRO\\SQLEXPRESS;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Table_Test";
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
SqlDataReader sdr = cmd.ExecuteReader();
while(sdr.Read())
{
chart1.Series["Series1"].Points.AddXY(sdr[0],sdr[1]);
}
con.Dispose();
}
这个是正常的。
private void Form1_Load(object sender, EventArgs e)
{
chart1.Series["Series1"].ChartType = SeriesChartType.Line;
string str="Data Source=WIN-NVQAG2H5CRO\\SQLEXPRESS;Integrated Security=True";
SqlConnection con = new SqlConnection(str);
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from Table_Test";
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
SqlDataReader sdr = cmd.ExecuteReader();
bool tf =sdr.Read();
while(tf)
{
chart1.Series["Series1"].Points.AddXY(sdr[0],sdr[1]);
}
con.Dispose();
}
请高人指点!
------解决方案--------------------------------------------------------
死循环了。
while(sdr.Read())
{
chart1.Series["Series1"].Points.AddXY(sdr[0],sdr[1]);
}
每次读一行,读到末尾sdr.Read返回false.循环退出。
bool tf =sdr.Read(); //读一行,如果表里有数据。tf为True
while(tf) //一直是True
{
chart1.Series["Series1"].Points.AddXY(sdr[0],sdr[1]); //一直读第一行
}
------解决方案--------------------------------------------------------
bool tf =sdr.Read();
while(tf)
{
chart1.Series["Series1"].Points.AddXY(sdr[0],sdr[1]);
}
con.Dispose();
}
如果 sdr有数据,那么 tf= true;
那么 while(true)
{
dosomething();
}
会一直循环下去。。。。。。这样做是不对的。。
而你第一个例子,
while(sdr.Read())
{
chart1.Series["Series1"].Points.AddXY(sdr[0],sdr[1]);
}
con.Dispose();
}
如果sdr有第一条数据,就读取第一条,并返回true,然后 sdr.read()第二次,读取第二条,如果第二条没有数据,while跳出
------解决方案--------------------------------------------------------
SqlDataReader.Read 没有理解它的含义