简体   繁体   English

参数 @Something 没有默认值(Access 数据库、Visual Studio VB.NET)

[英]Parameter @Something Has No Default Value (Access Database, Visual Studio VB.NET)

I am using an Access Database to store records of occurrences of errors to track their quantity and frequency.我正在使用 Access 数据库来存储错误发生的记录,以跟踪错误的数量和频率。 Thanks to some great help from some gifted programmers on this site, I am now getting closer to my end goal of pulling the occurrence counts back out of the database, and into charts in an Visual Studio program.多亏了这个网站上一些有天赋的程序员的大力帮助,我现在离我的最终目标越来越近了,即将出现次数从数据库中提取出来,并放入 Visual Studio 程序的图表中。

The problem I'm running into is that there is an error being thrown at this line in my function:我遇到的问题是我的 function 中的这一行抛出错误:

count = command.ExecuteScalar()计数 = command.ExecuteScalar()

The error is as follows:错误如下:

System.Data.OleDb.OleDbException: 'Parameter @MY_OF_LOG has no default value.' System.Data.OleDb.OleDbException:“参数 @MY_OF_LOG 没有默认值。”

I am successfully returning a value for count, which is 0. This is a possible value, but I am now wondering if the fact that the count returns as zero is what might be partially contributing to this error?我成功地为计数返回了一个值,即 0。这是一个可能的值,但我现在想知道计数返回为零的事实是否是导致此错误的部分原因?

Note: the values that will be returned from these counts for each month will be used as y values for a chart.注意:每个月从这些计数中返回的值将用作图表的 y 值。

Does anyone have any ideas on why this error occurred?有谁知道为什么会发生此错误? I will insert a snippet of my code below.我将在下面插入我的代码片段。 Thanks for the help.谢谢您的帮助。

Private Function CountMissedParts() Handles 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

A small caveat: I don't have much experience with Charts, but the other stuff should be fine.一个小警告:我对图表没有太多经验,但其他东西应该没问题。

The important parts are:重要的部分是:

 Dim logDates() As DateTime...

This declares an array of DateTime.这声明了一个 DateTime 数组。 An array is a simple way to store multiple values of the same type when you intend to loop through the values.当您打算遍历值时,数组是一种存储相同类型的多个值的简单方法。 I also took the liberty of using the VB.Net DateTime type to explicitly state the Year, Month, and Date values.我还冒昧地使用 VB.Net DateTime 类型来显式 state 年、月和日期值。 This method will make things much clearer for you and others reading your code.这种方法将使您和阅读您的代码的其他人更加清楚。

 For counter As Integer = 0 To logDates.Count - 1

The database command is executed 12 times, once for each entry in the array.数据库命令执行 12 次,对数组中的每个条目执行一次。

 Using Command As New OleDbCommand(sql, connection) Command.Parameters.Add("MY_OF_LOG", OleDbType.Date).Value = logDates(counter) Dim returnValue = Command.ExecuteScalar() Dim count As Integer = Integer.Parse(returnValue.ToString) ' other points NotInEpicorCHRT.Series("Missed Part").Points.AddXY(counter + 1, count) End Using

The return value of ExecuteScalar is then put in to the Chart series using the index you originall然后使用您原来的索引将 ExecuteScalar 的返回值放入图表系列


The whole code suggestion is below:整个代码建议如下:

    Public Sub CountMissedParts()
        Dim logDates() As DateTime = {New DateTime(2021, 1, 1),
                                      New DateTime(2021, 2, 1),
                                      New DateTime(2021, 3, 1),
                                      New DateTime(2021, 4, 1),
                                      New DateTime(2021, 5, 1),
                                      New DateTime(2021, 6, 1),
                                      New DateTime(2021, 7, 1),
                                      New DateTime(2021, 8, 1),
                                      New DateTime(2021, 9, 1),
                                      New DateTime(2021, 10, 1),
                                      New DateTime(2021, 11, 1),
                                      New DateTime(2021, 12, 1)}

        ' set 0,0
        NotInEpicorCHRT.Series("Missed Part").Points.AddXY(0, 0)

        Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb")
            connection.Open()

            For counter As Integer = 0 To logDates.Count - 1
                Dim sql = $"SELECT COUNT(*) FROM DataCollection WHERE [MISSED PART] = 'Missed Part' AND [M/Y OF LOG] = @MY_OF_LOG;"
                Using Command As New OleDbCommand(sql, connection)
                    Command.Parameters.Add("MY_OF_LOG", OleDbType.Date).Value = logDates(counter)
                    Dim returnValue = Command.ExecuteScalar()

                    Dim count As Integer = Integer.Parse(returnValue.ToString)

                    ' other points
                    NotInEpicorCHRT.Series("Missed Part").Points.AddXY(counter + 1, count)
                End Using
            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
    End Sub

Since you already accepted an answer, I assumed your problem was solved.由于您已经接受了答案,我认为您的问题已解决。 My function would look like this.我的 function 看起来像这样。

Private Function CountMissedParts() As DataTable
    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];"
    Using dt As New DataTable
        Using connection As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\Database1.accdb"),
        command As New OleDbCommand(sql, connection)
            connection.Open()
            Using reader = command.ExecuteReader
                dt.Load(reader)
            End Using
        End Using
        Return dt
    End Using
End Function

Usage.用法。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim dt = CountMissedParts()
    'Add a grid to your form just to see what data was returned
    'If all is well delete grid and the following line
    DataGridView1.DataSource = dt
    'To show you how to access the data
    'Inside the For loop is where you would put the code to fill you graph
    For Each row As DataRow In dt.Rows
        Debug.Print($"{row("M/Y OF LOG")}  -  {row("Total")}")
    Next
End Sub

BTW, if the previous answer didn't work, you can click the check again and choose another answer.顺便说一句,如果之前的答案不起作用,您可以再次单击检查并选择另一个答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 参数无默认值 vb.net - parameter has no default value vb.net Visual Studio(vb.net)无法加载访问数据库 - visual studio (vb.net) not able to load an access database 使用Visual Studio 2015更新Access数据库-VB.net - Update Access database using Visual Studio 2015 - VB.net 如何使用 Visual Studio 计算 Access 数据库中的值 VB.NET - How to Count Values in Access Database using Visual Studio VB.NET 如何通过VB.net从Visual Studio将多个ComboBox值输入到Access数据库中 - How to input multiple ComboBox values into Access Database from Visual Studio via VB.net 无法使用 Visual Studio 通过 VB.NET 中的连接字符串手动访问本地 SQL 数据库 - Can't access Local SQL database manually by connection string in VB.NET using Visual Studio 如何使用 Visual Studio 2019 (VB.NET) 按钮在 Access 数据库中创建新记录 - How to Create a New Record in an Access Database using Visual Studio 2019 (VB.NET) Button Click 在 VB.NET (Visual Studio 2013) 中使用 OLEDB 通过 DataGridView 更新访问数据库的按钮 - Update Button for Access Database via DataGridView Using OLEDB in VB.NET (Visual Studio 2013) Visual Studio 2015 VB.net如何在MS Access数据库中查找记录并禁用表单中的按钮? - Visual Studio 2015 VB.net How to find record in MS Access database and disable button in form? 如何从 Visual Studio 将新记录添加到 Access 数据库。 (VB.NET) - How to Add New Records to An Access Database from Visual Studio. (VB.NET)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM