0

我使用 vb.net 和 sql 作为我的数据库。我将如何绘制折线图?通过使用这个 sql 语句

我的 X 轴将是来自 Time_stamp 的范围值,它将由用户键入,我将其分配为 = 从 2013 年 6 月 24 日上午 8:38:00 到 2013 年 6 月 24 日上午 8:38:23 和我的Y 轴将是 BB_ID,将由用户键入,我将其分配为 = 3100

这是我的一般代码:

    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()
4

1 回答 1

0

无需更改为 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();

在您的代码中

Chart1.Series("line_area").Points.AddXY("3100", "10")
Chart1.Series("line_area").Points.AddXY("3100", "8")

您只是在图表中添加两个点,仅此而已,数据库中的数据根本没有连接到图表。

于 2013-07-26T11:42:42.367 回答