如何通过从 vb.net 中的数据库 (sql) 读取值来绘制折线图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17288682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to plot a line graph by reading the values from database (sql) in vb.net?
提问by Husna5207
i'm using vb.net and sql as my database. how i'm going to plot a line graph ? by using this sql statement
我使用 vb.net 和 sql 作为我的数据库。我将如何绘制折线图?通过使用这个 sql 语句
my X axis will be the range value from Time_stamp that will be key-in by user which i assign that as = from 6/24/2013 8:38:00 AM to 6/24/2013 8:38:23 AM and my Y axis will be BB_ID that will key-in by user and i assign that as = 3100
我的 X 轴将是来自 Time_stamp 的范围值,该值将由用户键入,我将其分配为 = from 6/24/2013 8:38:00 AM to 6/24/2013 8:38:23 AM 和我的Y 轴将是 BB_ID,它将由用户键入,我将其分配为 = 3100
this is my general code :
这是我的通用代码:
Dim connectionString As String = "server='abc'; user id='***'; password='***'; Database='***'"
Dim sqlConnection As SqlClient.SqlConnection = New SqlClient.SqlConnection(connectionString)
sqlConnection.Open()
Dim queryString As String = "SELECT Time_stamp, BB_ID, Status, " & _
"(CASE WHEN Status = 'R' THEN 0 WHEN status = 'O' THEN 1 ELSE 2 END) AS newstatus"& _
"FROM(dbo.rawdata)" & _
"WHERE (BB_ID = '3100') AND (Time_stamp >= '6/24/2013 8:38:00 AM') AND (Time_stamp <= '6/24/2013 8:38:23 AM')"
Chart1.Series("line_area").Points.AddXY("3100", "10")
Chart1.Series("line_area").Points.AddXY("3100", "8")
Chart1.Series("line_area").ChartType = System.Web.UI.DataVisualization.Charting.SeriesChartType.Line
sqlConnection.Close()
采纳答案by Joe Vi
There is no need to change to ZedGraph. MSChart works fine in this case too. You have created a query string, but you are not querying the database.
无需更改为 ZedGraph。MSChart 在这种情况下也能正常工作。您已经创建了一个查询字符串,但不是在查询数据库。
string connectionString = // your connection string
string queryString = // your query string
// Create a database connection object using the connection string
SQLConnection sqlConnection = // your connection object
// Create a database command on the connection using query
SQLCommand sqlCommand = // your command
sqlConnection .Open();
// set chart data source - the data source must implement IEnumerable
chart1.DataSource = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
// Set series members names for the X and Y values
chart1.Series["line_area"].XValueMember = "Time_stamp"; // the column you want on X axis
chart1.Series["line_area"].YValueMembers = "BB_ID"; // the column you want on Y axis
// Data bind to the selected data source
chart1.DataBind();
In your code
在你的代码中
Chart1.Series("line_area").Points.AddXY("3100", "10")
Chart1.Series("line_area").Points.AddXY("3100", "8")
you are just adding two points to the chart, thats all, the data from the database is not connected to the chart at all.
您只是在图表中添加了两个点,仅此而已,数据库中的数据根本没有连接到图表。

