使用VB中的访问数据库中的数据查看图表

时间:2016-04-10 08:42:29

标签: vb.net ms-access

我想查看数据库中数据的图表 我尝试了这段代码,但收到了错误信息

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Chart1.Series.Add("Total number of Orders for each Company")

    Dim Conn As OleDbConnection = New OleDbConnection
    Dim provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
    Dim dataFile = "F:\College Files\Co-op Files\Aramco Project\Application\OrdersApplication\OrdersApplication\Database.accdb"
    Conn.ConnectionString = provider & dataFile
    Conn.Open()

    Dim cmd As OleDbCommand = New OleDbCommand("SELECT [CompanyID], (SELECT COUNT(QuantityRequired) FROM Order_Details) FROM [Orders] ", Conn)
    Dim dr As OleDbDataReader = cmd.ExecuteReader
    While dr.Read
        Chart1.Series("Total number of Orders for each Company").Points.AddY(dr("CompanyID").ToString, dr("QuantityRequired").ToString)
    End While
    dr.Close()
    cmd.Dispose()

End Sub

错误消息表明QuantityRequired是此行中的附加信息

        Chart1.Series("Total number of Orders for each Company").Points.AddY(dr("CompanyID").ToString, dr("QuantityRequired").ToString)

当我删除COUNT时,它会显示,但我需要它来计算每个公司ID所需的数量

1 个答案:

答案 0 :(得分:0)

你错过了给出字段名称的别名:

Dim cmd As OleDbCommand = New OleDbCommand("SELECT [CompanyID], (SELECT COUNT(*) FROM Order_Details) As QuantityRequired FROM [Orders] ", Conn)

猜猜。 SQL读取:

"SELECT [CompanyID], (SELECT COUNT(*) FROM Order_Details AS T WHERE T.OrderId = Orders.Id) As QuantityRequired FROM [Orders]"