如何从Access数据库中获取带数量的畅销产品并显示在标签上?

问题描述 投票:-1回答:1

我正在做一个POS系统,其中,我必须包括前3个销售产品的数量在仪表板上,这是我无法找出如何做到这一点,请帮助我做编码和数据应该检索的形式访问数据库。

这是我做了什么,以获得顶级1销售项目。我需要得到顶部第2个项目和第3个项目也。

结果期望值的图像Image of the result expectation

con = New OleDbConnection(cs)
        con.Open()
        Dim ct As String = "SELECT TOP 3 ProductName, Sum(Quantity) as Quantity FROM ProductSoldDB group by ProductName order by SUM(Quantity)"
        cmd = New OleDbCommand(ct, con)

        rdr = cmd.ExecuteReader()
        While rdr.Read
            lblProduct1Name.Text = rdr("ProductName")
            lblQTYProduct1.Text = rdr("Quantity")
        End While
        If Not rdr Is Nothing Then
            rdr.Close()
        End If

        con.Close()
vb.net access
1个回答
1
投票

@Always_a_learner做了艰苦的工作,得到一个工作的Select statment。

我不喜欢在更新用户界面时保持连接打开。所以,我填写了一个 DataTable 并将其传递给 Button.Click 从而将数据访问代码与用户界面分离。

连接和命令除了封闭外,还需要进行处置,所以用 Using...End Using 块。

Private Function RetrieveData() As DataTable
    Dim sql = "SELECT TOP 10 ProductName, Sum(Quantity) as Quantity FROM ProductSoldDB Group By ProductName Order By SUM(Quantity)"
    Dim dt As New DataTable
    Using cn As New OleDbConnection("Your connection string"),
            cmd As New OleDbCommand(sql, cn)
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    Return dt
End Function

我创建了一个Labels数组,这样我们就可以循环地从 DataTable. i 是该行的索引,每次迭代时递增1。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt = RetrieveData()
    Dim LabelsToFill = {Label1, Label2, Label3, Label4, Label5, Label6, Label7, Label8, Label9, Label10}
    Dim i As Integer
    For Each l As Label In LabelsToFill
        l.Text = $"{dt.Rows(i)("ProductName")} - {dt.Rows(i)("Quantity")}"
        i += 1
    Next
End Sub

要填写2个不同的标签,一个是数量,一个是名称......

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Dim dt = RetrieveData()
    Dim QuanityLabels = {QLabel1, QLabel2, QLabel3, QLabel4, QLabel5, QLabel6, QLabel7, QLabel8, QLabel9, QLabel10}
    Dim NameLabels = {NLabel1, NLabel2, NLabel3, NLabel4, NLabel5, NLabel6, NLabel7, NLabel8, NLabel9, NLabel10}
    Dim i As Integer
    For i = 0 To 9
        NameLabels(i).Text = dt.Rows(i)("ProductName").ToString
        QuanityLabels(i).Text = dt.Rows(i)("Quantity").ToString
    Next
End Sub

编辑

错 $. $表示插值字符串的开头,如果被大括号{ }包围,可以直接嵌入变量。这在vb.net中与Visual Studio 2015一起变得可用。在以前的版本中,你可以使用String.Format来实现同样的结果。

l.Text = String.Format("{0} - {1})", dt.Rows(i)("ProductName"), dt.Rows(i)("Quantity"))

1
投票

比方说,你有3个面板来显示前3名的销售产品,并且每个面板都有级别:面板1-标签产品名称-lblProduct1Name ,标签数量名称-lblQTYProduct1。

面板2--标签产品名称--lblProduct2Name,标签数量名称--lblQTYProduct2。

面板3--标注产品名称--lblProduct3Name,标注数量名称--lblQTYProduct3。

我们将在每次迭代中得到lblProductName和lblQTYProduct,并为它们设置值。

最终代码如下。

    con = New OleDbConnection(cs)
    con.Open()
    Dim ct As String = "SELECT TOP 3 ProductName, Sum(Quantity) as Quantity FROM ProductSoldDB group by ProductName order by SUM(Quantity) Desc"
    cmd = New OleDbCommand(ct, con)

    rdr = cmd.ExecuteReader()
    Dim index As Integer = 1
    While rdr.Read

        Dim ctrlProduct As Control = Me.Controls.Find("lblProduct" + index.ToString() + "Name", True).FirstOrDefault()

        ctrlProduct.Text = rdr("ProductName")

        Dim ctrlQuantity As Control = Me.Controls.Find("lblQTYProduct" + index.ToString(), True).FirstOrDefault()

        ctrlQuantity.Text = rdr("Quantity").ToString()
        index = index + 1

    End While
    If Not rdr Is Nothing Then
        rdr.Close()
    End If

    con.Close()

输出--根据你的要求改变外观和感觉。enter image description here


0
投票

这是最后的代码,在@mary的帮助下,解决了我的问题。

Private Function RetrieveData() As DataTable
        Dim sql = "SELECT TOP 3 ProductName, Sum(Quantity) as Quantity FROM ProductSoldDB group by ProductName order by Sum(Quantity) DESC"
        Dim dt As New DataTable
        Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Desktop\EME.accdb;Persist Security Info=False;"),
                cmd As New OleDbCommand(sql, cn)
            cn.Open()
            dt.Load(cmd.ExecuteReader)
        End Using
        Return dt
    End Function
Private Sub Load_TopSellingProducts()

        Dim dt = RetrieveData()
        Dim QuanityLabels = {lblQTYProduct1, lblQTYProduct2, lblQTYProduct3}
        Dim NameLabels = {lblProductName1, lblProductName2, lblProductName3}
        Dim i As Integer
        For i = 0 To 2
            NameLabels(i).Text = dt.Rows(i)("ProductName").ToString
            QuanityLabels(i).Text = dt.Rows(i)("Quantity").ToString
        Next
    End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Load_TopSellingProducts()
    End Sub

最后,我得到了我所期望的结果

结果

© www.soinside.com 2019 - 2024. All rights reserved.