'假如是第二次載入
If Page.IsPostBack Then
Chart1.Visible = True
Dim date_s, date_e, type As String
Dim conn = New SqlConnection
Dim cmd As SqlCommand
Dim dr As SqlDataReader
Dim dt As DataTable = New DataTable
Dim SqlString As String
date_s = TextBox1.Text
date_e = TextBox2.Text
type = DropDownList1.SelectedValue
'date_s = "2013-08-21 16:48:00.000"
'date_e = "2013-08-21 17:50:00.000"
'type = "Co2"
conn.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString() '使用web config
SqlString = "select * from environment_log " & _
" where CONVERT (VARCHAR(10), timestamp, 111) >='" + date_s & _
"' and CONVERT (VARCHAR(10), timestamp, 111) <= '" + date_e & _
"' and type='" + type + "' order by timestamp"
'Label1.Text = SqlString
cmd = New SqlCommand(SqlString, conn) '定義cmnd為SqlCommand指令
'comnd.ExecuteNonQuery() '執行
Try
conn.Open() '開啟連結
dr = cmd.ExecuteReader() '讀取
If dr.HasRows = True Then
'第一種datareader
塞入datatable,使用多個線圖仍須測試
dt.Load(dr)
''Use linq to datatable
'Dim search1 = From c In dt.AsEnumerable() _
'Where c.Field(Of String)("DriverID") = "temp1" _
'Select c
'Dim temp1 = search1.CopyToDataTable()
'Dim search2 = From c In dt.AsEnumerable() _
'Where c.Field(Of String)("DriverID") = "temp2" _
'Select c
'Dim temp2 = search2.CopyToDataTable()
'Chart1.DataSource = temp2
'Chart1.Series("Series1").XValueMember = "timestamp"
'Chart1.Series("Series1").YValueMembers = "value"
''Chart1.Series("Series2").XValueMember = "timestamp"
''Chart1.Series("Series2").YValueMembers = "value"
'第二種使用databindcrosstable
'Use DataBindCrossTable
Chart1.DataBindCrossTable(dt.Rows, "DriverID", "timestamp", "value", "")
'不使用下列方式,只會跑長條圖..
For Each cs As Series In Chart1.Series
cs.ChartType = SeriesChartType.Spline
'cs.IsValueShownAsLabel = True
Next
Chart1.DataBind()
End If
Catch ex As Exception
Response.Write(e.ToString())
Finally
If Not (dr Is Nothing) Then ' --關閉 DataReader
cmd.Cancel() '--自己補上的,原因在下面有講。
dr.Close()
End If
If (conn.State = ConnectionState.Open) Then ' --關閉 DB的連線
conn.Close()
End If
End Try
End If
0 意見:
張貼留言