简体   繁体   English

从选择查询mysql vb.net插入值数组

[英]Insert Array of values from Select query mysql vb.net

I have an application that shows the shift target of a certain reference. 我有一个显示特定参考的转换目标的应用程序。 I have a lot of references divided in 3 shifts (morning, afternoon, evening) The app is working good but now i want to reset the counter of the shift targets in the beginning of every shift for all references. 我将很多参考分为3个班次(上午,下午,晚上),该应用程序运行良好,但现在我想在每个班次开始时为所有参考重新设置班次目标的计数器。 I mean, when the clock is 6am, all the morning shift should put the counter on 0. I have the following code but is not working. 我的意思是,当时钟是上午6点时,所有早上的班次都应将计数器设置为0。我有以下代码,但无法正常工作。 Something is missing and i'm stuck. 缺少某些东西,我被卡住了。

Dim rtime As New TimeSpan(0, 0, CInt(requestddate.TimeOfDay.TotalSeconds))


        hora1 = New TimeSpan(6, 0, 0)
        hora2 = New TimeSpan(14, 0, 0)
        hora3 = New TimeSpan(22, 0, 0)

        ConnectDatabase()

        If rtime = hora1 Then

            count = 0

            Try

                With cmd4
                    .Connection = conn
                    .CommandText = "Select ShiftTarget.IDShiftTarget from ShiftTarget, turnos, linhas WHERE ShiftTarget.IDLinha = linhas.IDLinha and ShiftTarget.IDTurno = turnos.IDTurno And turnos.Descricao = 'Manha' And linhas.NomeLinha = '" & GlobalVariables.linha & "'"
                End With

                While objReader.Read()
                listaIDs.Add(objReader("IDShiftTarget").ToString())
            End While

            objReader.Close()

            DisconnectDatabase()

            ConnectDatabase()

            With cmd1
                .Connection = conn
                .CommandText = "INSERT into contador (IDShiftTarget, Data, Contador) VALUES (@ID, @date, @cont)"
                For Each item As String In listaIDs
                    MsgBox(item)
                    .Parameters.AddWithValue("@ID", item)
                    .Parameters.AddWithValue("@date", data.ToString("yyyy-MM-dd"))
                    .Parameters.AddWithValue("@cont", count)
                    .ExecuteNonQuery()
                Next
            End With

        Catch ex As Exception
            If ex.InnerException IsNot Nothing Then
                MsgBox(ex.InnerException)
            End If
        End Try

        DisconnectDatabase()

Can someone tell me what am i doing wrong? 有人可以告诉我我在做什么错吗?

Update: This is what i get (and it's expected) from the select query: 更新:这是我从选择查询中得到的(也是预期的): 在此处输入图片说明

And this is the table where i want to reset the counter: 这是我要重置计数器的表: 在此处输入图片说明

So, imagine, when the actual time is 6am, i want to insert something like: 因此,想象一下,当实际时间是上午6点时,我想插入类似以下内容:

IDContador   IDShiftTarget      Data      Contador
11933          32           2016-02-23       0
11934          19           2016-02-23       0
11935          20           2016-02-23       0
11936          35           2016-02-23       0

I cannot simply reset to 0 where idshifttarget is equal to 19,20,32,35 because that would reset all the records, and i want to keep them in the table. 我不能简单地重置为0,其中idshifttarget等于19、20、32、35,因为那样会重置所有记录,并且我想将它们保留在表中。 I need to check the MAX(ID) where the shiftTarget is the value i got in the array, and then put the Contador to 0. 我需要检查MAX(ID),其中shiftTarget是我在数组中获得的值,然后将Contador设置为0。

UPDATE: 更新:

I have made this changes in the code: 我已经在代码中进行了更改:

While objReader.Read()
                listaIDs.Add(objReader("IDShiftTarget").ToString())
            End While

            objReader.Close()

            DisconnectDatabase()

            ConnectDatabase()

            With cmd1
                .Connection = conn
                .CommandText = "INSERT into contador (IDShiftTarget, Data, Contador) VALUES (@ID, @date, @cont)"
                For Each item As String In listaIDs
                    MsgBox(item)
                    .Parameters.AddWithValue("@ID", item)
                    .Parameters.AddWithValue("@date", data.ToString("yyyy-MM-dd"))
                    .Parameters.AddWithValue("@cont", count)
                    .ExecuteNonQuery()
                Next
            End With

        Catch ex As Exception
            If ex.InnerException IsNot Nothing Then
                MsgBox(ex.InnerException)
            End If
        End Try

        DisconnectDatabase()

With this code i can only see the first ID and in the console i get the next error: Exception thrown: 'System.InvalidOperationException' in MySql.Data.dll 使用此代码,我只能看到第一个ID,在控制台中,我会收到下一个错误:抛出异常:MySql.Data.dll中的'System.InvalidOperationException'

There are some things in your code which lead to wrong havior: 您的代码中有些东西会导致错误的行为:

  • You cannot execute a CRUD action while looping thru a DataReader: Create a list to store IDShiftTargets , close the DataReader and then loop thru the list and execute the Insert statements. 在循环通过DataReader时不能执行CRUD操作:创建一个列表以存储IDShiftTargets ,关闭DataReader,然后在列表中循环并执行Insert语句。
  • Take care that the data types of your DB columns match with the code data types (IDShiftTarget int/Integer, IDShiftTarget date/DateTime, ...). 请注意,数据库列的数据类型与代码数据类型(IDShiftTarget int / Integer,IDShiftTarget date / DateTime等)匹配。
  • When using a prepared statement with bound parameters make sure that you use AddWithValue only once outside the loop and set it in the loop with Parameter("@ID").Value = item 当使用带有绑定参数的预备语句时,请确保仅在循环外部使用AddWithValue一次,并在循环中使用Parameter("@ID").Value = item
  • Since the code is executed in a timer event which ticks every second, it is 由于代码是在计时器事件中执行的,该事件每秒钟滴答一次,因此它是
    likely that a first execution has not finished until the next timer ticks. 可能直到下一个计时器计时才完成第一次执行。 If you want to run the code secure and synchron make sure that the current execution is not disturbed, eg setting Timer.Enabled= False at the beginning of the function and Timer.Enabled=True after function has finished. 如果要安全运行代码并进行同步,请确保不干扰当前执行,例如,在函数开始处设置Timer.Enabled= False ,在函数完成后设置Timer.Enabled=True Or just set the timer tick high enough ( d'oh ). 或者只是将计时器刻度线设置得足够高( d'oh )。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM