微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用 Visual Studio VB.NET 计算 Access 数据库中的值

如何解决如何使用 Visual Studio VB.NET 计算 Access 数据库中的值

我有一个访问数据库,该数据库将数据存储在由 Visual Studio 程序记录的表 (称为 DataCollection) 中。我一直在努力寻找解决方案来计算特定列中包含特定值的记录数量

例如,在我的数据库中,有两列。一列标题为“M/Y OF LOG”,它返回格式为“1/1/2021”的日期。另一列标题为“MISSED PART”,它只能返回两个值,“Missed Part”或“neatoL”。理想情况下,我想使用 Visual Studio 计算某个月份出现“漏件”的次数

有没有人对代码的外观有任何想法,或者是否可以这样计算?

我的连接字符串:

connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"

我的 Access 数据库格式预览:

您会看到列比提到的多得多。请忽略“M/Y of Log”和“Missed Part”以外的任何列

enter image description here

我的代码中有这些变量。它们将是从数据库获取返回值的变量,用作图表中的 y 值以显示数据:

私有函数 CountMissedParts() 处理 MyBase.Load

    Dim sql = $"SELECT COUNT(*)
        FROM DataCollection
        WHERE [MISSED PART] = 'Missed Part'
        AND [M/Y OF LOG] = @MY_OF_LOG;"
    Dim JANmyOfLog = #1/1/2021#
    Dim FEBmyOfLog = #2/1/2021#
    Dim MARmyOfLog = #3/1/2021#
    Dim APRmyOfLog = #4/1/2021#
    Dim MAYmyOfLog = #5/1/2021#
    Dim JUNmyOfLog = #6/1/2021#
    Dim JULmyOfLog = #7/1/2021#
    Dim AUGmyOfLog = #8/1/2021#
    Dim SEPmyOfLog = #9/1/2021#
    Dim OCTmyOfLog = #10/1/2021#
    Dim NOVmyOfLog = #11/1/2021#
    Dim DECmyOfLog = #12/1/2021#
    Dim count As Integer
    Dim JanuaryMP As Double
    Dim FebruaryMP As Double
    Dim marchMP As Double
    Dim AprilMP As Double
    Dim MayMP As Double
    Dim JuneMP As Double
    Dim JulyMP As Double
    Dim AugustMP As Double
    Dim SeptemberMP As Double
    Dim OctoberMP As Double
    Dim NovemberMP As Double
    Dim DecemberMP As Double

    Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"),command As New OleDbCommand(sql,connection)
        JanuaryMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = JANmyOfLog
        FebruaryMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = FEBmyOfLog
        marchMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = MARmyOfLog
        AprilMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = APRmyOfLog
        MayMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = MAYmyOfLog
        JuneMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = JUNmyOfLog
        JulyMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = JULmyOfLog
        AugustMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = AUGmyOfLog
        SeptemberMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = SEPmyOfLog
        OctoberMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = OCTmyOfLog
        NovemberMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = NOVmyOfLog
        DecemberMP = command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = DECmyOfLog
        connection.open()
        count = command.ExecuteScalar()




        ' set 0,0
        notinEpicorCHRT.Series("Missed Part").Points.AddXY(0,0)
        ' other points
        notinEpicorCHRT.Series("Missed Part").Points.AddXY(1,JanuaryMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(2,FebruaryMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(3,marchMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(4,AprilMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(5,MayMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(6,JuneMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(7,JulyMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(8,AugustMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(9,SeptemberMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(10,OctoberMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(11,NovemberMP)

        notinEpicorCHRT.Series("Missed Part").Points.AddXY(12,DecemberMP)

    End Using
    notinEpicorCHRT.ChartAreas(0).AxisX.Minimum = 0.0
    notinEpicorCHRT.ChartAreas(0).AxisX.Maximum = 12
    notinEpicorCHRT.ChartAreas(0).AxisX.Interval = 1
    notinEpicorCHRT.ChartAreas(0).AxisY.Minimum = 0.0
    notinEpicorCHRT.ChartAreas(0).AxisY.Maximum = 45
    notinEpicorCHRT.ChartAreas(0).AxisY.Interval = 5
End Function

解决方法

Dim sql = $"SELECT COUNT(*)
            FROM DataCollection
            WHERE [MISSED PART] = 'Missed Part'
            AND [M/Y OF LOG] = @MY_OF_LOG"
Dim myOfLog = #1/01/2021#
Dim count As Integer

Using connection As New OleDbConnection("connection string here"),command As New OleDbCommand(sql,connection)
    command.Parameters.Add("@MY_OF_LOG",OleDbType.Date).Value = myOfLog
    connection.Open()
    count = CInt(command.ExecuteScalar())
End Using

该示例使用硬编码日期,但您可以从任何您喜欢的位置获取日期值。 DateDate

,

不需要每个月都定义变量,可以用循环代替。

检查以下代码:

    Dim count As Integer
    Dim lst As List(Of Double) = New List(Of Double)

    Dim sql = $"SELECT COUNT(*) FROM DataCollection WHERE [MISSED PART] = 'Missed Part' AND [M/Y OF LOG] = @MY_OF_LOG;"

    Using connection As New OleDbConnection("your connection String"),connection)
        connection.Open()

        For i As Integer = 1 To 12
            command.Parameters.Clear()
            Dim dtime = New DateTime(2021,i,1).ToString("MM/dd/yyyy")
            command.Parameters.AddWithValue("@MY_OF_LOG",dtime)
            count = command.ExecuteScalar()
            lst.Add(count)
        Next

        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(0,0)
        For j As Integer = 1 To lst.Count
            NotInEpicorCHRT.Series("Missed Part").Points.AddXY(j,lst(j - 1))
        Next

    End Using
    NotInEpicorCHRT.ChartAreas(0).AxisX.Minimum = 0.0
    NotInEpicorCHRT.ChartAreas(0).AxisX.Maximum = 12
    NotInEpicorCHRT.ChartAreas(0).AxisX.Interval = 1
    NotInEpicorCHRT.ChartAreas(0).AxisY.Minimum = 0.0
    NotInEpicorCHRT.ChartAreas(0).AxisY.Maximum = 45
    NotInEpicorCHRT.ChartAreas(0).AxisY.Interval = 5
,

好吧,我假设日期列实际上是一个实际的日期列。这样,您的日期显示无关紧要。作为一般规则,所有日期都以内部格式存储 - 您不关心外部格式。

所以,我们真正需要的是您想要的月份和年份。

因此,构建一个带有两个文本框的表单。一个文本框是年份,另一个是月份。

这样说:

enter image description here

所以,这段代码会提示你输入年份,然后是月份,然后输出结果。

按钮代码可以是这个,它会给出 Missed 和 NeatOL 的总数

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

    Dim strSQL As String

    strSQL = "SELECT abs(SUM([Missed Part] = 'Missed Part')) As MissPCount " &
             "abs(SUM([Missed Part] = 'NEATOL')) As MissNCount " &
             "FROM DataCollection WHERE Month([M/Y OF LOG]) = @Month " &
             "AND Year([M/Y OF LOG]) = @Year"

    Using cmdSQL As New OleDbCommand(strSQL,New OleDbConnection(My.Settings.TestDB))

        cmdSQL.Parameters.Add("@Month",OleDbType.Integer).Value = txtMonth.Text
        cmdSQL.Parameters.Add("@Year",OleDbType.Integer).Value = txtYear.Text

        cmdSQL.Connection.Open()
        Dim rstTable As New DataTable

        rstTable.Load(cmdSQL.ExecuteReader)

        ' display the reuslts in the two text boxes.
        With rstTable.Rows(0)

            txtNeatCount.Text = .Item("MissNCount")
            txtPartCount.Text = .Item("MissPCount")

        End With

    End Using

End Sub

编辑:

作为跟进?看起来您有一个始终设置为每月 1 日的单列。给这个,我用过month()和year()函数吗?嗯,使用 month() 和 year() 非常适合说谁有这个月的生日,(我们可以使用 month() 和 day() 提取月、日并忽略年份。

但是,我们仍然应该包含一个有效的日期范围。原因当然是“索引”或所谓的术语“sargable”。上面写的查询不能对结果使用索引。因此,对于大表,发布的查询将运行得非常慢。

假设在 MY/Y 列上有一个索引,那么执行 SQL 查询会更好(并且更快)。因此,我建议使用此解决方案:

    strSQL = "SELECT abs(SUM([Missed Part] = 'Missed Part')) As MissPCount " &
             "abs(SUM([Missed Part] = 'NEATOL' As MissNCount " &
             "FROM DataCollection WHERE [M/Y OF LOG] = @MyDate "

    Dim dtMyDate As Date = DateSerial(txtYear.Text,txtMonth.Text,1)

    Using cmdSQL As New OleDbCommand(strSQL,New OleDbConnection(My.Settings.TestDB))

        cmdSQL.Parameters.Add("@MyDate",OleDbType.DBDate).Value = dtMyDate

        cmdSQL.Connection.Open()
etc. etc. etc. etc.
,

我认为以下代码将为您提供所需的数据。如果您需要将数据表列转换为图表,请再问一个问题。

Private Sub OPCode()
    Dim sql = $"SELECT [M/Y OF LOG],COUNT([Missed Part]) As Total
                FROM [Data Collection]
                WHERE [MISSED PART] = 'MISSED PART'
                GROUP BY [M/Y OF LOG] 
                ORDER BY [M/Y OF LOG];"
    Dim dt As New DataTable
    Using cn As New OleDbConnection(My.Settings.DataCollection),cmd As New OleDbCommand(sql,cn)
        cn.Open()
        dt.Load(cmd.ExecuteReader)
    End Using
    DataGridView1.DataSource = dt
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。