2

我正在使用 C# winforms .NET4 应用程序

我的目标是从特定的 SQL 查询中填充图表控件。

“从人那里选择身高”

简单的查询,但我不确定将查询结果放入图表控件的最佳方法。

继承人我到目前为止:

private void button1_Click(object sender, EventArgs e)
        {         
            SqlConnection mainconnection = new SqlConnection("Data Source=commandhq;Initial Catalog=projecttest;Integrated Security=True");
            mainconnection.Open();
            string query = "SELECT height from persons";
            SqlCommand querycommand = new SqlCommand(query, mainconnection);
            SqlDataReader querycommandreader = querycommand.ExecuteReader();


            Series thesqlresultsplease = new Series();

            //Populate series with results from the sql query

            chart1.Series[0].XValueMember = thesqlresultsplease.XValueMember; //will this work?

        }

我的问题是:

我如何将 sqldatareader 的结果放入系列中。如果您知道更好的解决方法,请告诉我。我即将开始一个项目,所以这将非常有帮助。

提前致谢

4

2 回答 2

1

从这个开始:

while (querycommandreader.Read()) 
{
    chart1.Series[0].Points.AddY(reader.GetDouble(0));
}

根据“HEIGHT”列的 Sql Server 数据类型,您可能会遇到GetDouble部件问题。因此,如果您这样做,请告诉我数据类型,我们会做对的。

The Read() method returns a boolean - true if there are still records being read, false if you've passed the last on in the set. That's why you can simply use while(querycommandreader.Read()) and in the loop's block do something with each row in the set.

The series' Points collection has an AddY method that just takes a double and adds a point to the series. The double we're passing to it comes from column zero of the current record of the reader. That's probably the easiest way to accomplish what you want.

于 2012-07-24T12:44:46.480 回答
0
while(querycommandreader.Read())
{
   chart1.Series[0].XValueMember = int.Parse(querycommandreader["height"]);
}
于 2012-07-24T11:38:17.120 回答