VB.net插入数据时出现循环问题

时间:2019-06-11 02:07:12

标签: sql-server vb.net

我有带有列Ending, PumpNo, NozzleNo, TankID的datagridview。 来自datagridview的数据示例:

PumpNo    NozzleNo    TankID       Ending

   1          1       1-BIODIESEL  *This part is where user input ending for every pumpno, nozzle, and tankid*

这是插入的示例。在第一次插入时,Opening将设置为0:

enter image description here

当您第二次插入时,它应该在datagridview中循环并获取所有详细信息并将其保存到数据库。然后,从数据库中的过往记录中select top 1 ending order by desc开始,这将是您第二次插入的开头。看到下面带有黄色突出显示的图片:

enter image description here

问题是

我刚刚从上一条记录中获得了Ending。如下所示,其中所有结尾均为8.9

enter image description here

我认为我在插入pumpno = @pumpno and nozzleno = @nozzleno and TankID = @tankid之类的东西之前应该先进行过滤,这样当我插入表格时,我将根据过滤的内容获得指定的前1个结尾。

我的代码:

  Dim Ending As Decimal
            'This is my code in selecting top 1 ending 
            SelectQuery = "SELECT TOP 1 Ending FROM DigitalTB WHERE PumpNo = @Pump1 AND NozzleNo = @Nozzle1 AND TankID = @Tank1 ORDER BY Ending DESC"
            cmd = New SqlCommand
            With cmd
                .Connection = Connections.cn
                .CommandText = SelectQuery
                .Parameters.Add(New SqlParameter("@Pump1", SqlDbType.NVarChar, 10))
                .Parameters.Add(New SqlParameter("@Nozzle1", SqlDbType.NVarChar, 10))
                .Parameters.Add(New SqlParameter("@Tank1", SqlDbType.NVarChar, 100))

                Dim pump1 As String = String.Empty
                Dim nozzle As String = String.Empty
                Dim tankid As String = String.Empty

我在datagridview中循环到这里,以便在选择查询中的前1个结尾时可以用作过滤器:

                For Each row As DataGridViewRow In dgvDigital.Rows
                    pump1 = row.Cells(1).Value
                    nozzle = row.Cells(2).Value
                    tankid = row.Cells(3).Value

                    .Parameters("@Pump1").Value = pump1
                    .Parameters("@Nozzle1").Value = nozzle
                    .Parameters("@Tank1").Value = tankid
                    .ExecuteNonQuery()

然后使用sqlreader读取从select top 1 ending查询中获得的所有内容。在这一部分中,当我msgbox(ending)可以正常工作时,但是当我在此处插入插入内容时,它将在第一行中选择前1个,然后插入和循环在这里不起作用。

                    Using read As SqlDataReader = cmd.ExecuteReader
                        If read.Read Then
                            Ending = read("Ending")
                            read.Close()
'Msgbox(Ending)
                        End If
                    End Using
                Next
            End With

这是我的插入代码,可在其中获取表中的详细信息并将其插入数据库。我想在这里有一个过滤器:

            'Inserting code
            InsertQuery = "INSERT INTO DigitalTB(PumpNo,NozzleNo,TankID,Opening,Ending,ReadingType,UserShift,Date,Time)" &
                " VALUES(@PumpNo1,@NozzleNo1,@TankID1,@Opening1,@Ending1,@ReadingType1,@UserShift1,@Date1,@Time1)"
            cmd = New SqlCommand
            With cmd
                .Connection = Connections.cn
                .CommandText = InsertQuery
                .Parameters.Add(New SqlParameter("@Ending", SqlDbType.Decimal))
                .Parameters.Add(New SqlParameter("@PumpNo", SqlDbType.NVarChar, 10))
                .Parameters.Add(New SqlParameter("@NozzleNo", SqlDbType.NVarChar, 10))
                .Parameters.Add(New SqlParameter("@TankID", SqlDbType.NVarChar, 100))

                .Parameters.AddWithValue("@Opening", Ending)  ' This part is where i insert the top 1 from ending
                .Parameters.AddWithValue("@UserShift", Interfacefrm.lblUserInterface.Text)
                .Parameters.AddWithValue("@ReadingType", "Fuel")
                .Parameters.AddWithValue("@Date", Date.Now.ToShortDateString) 'I save it as string
                .Parameters.AddWithValue("@Time", Date.Now.ToShortTimeString) 'I save it as string
            End With

            For i As Integer = 0 To Me.dgvDigital.Rows.Count - 1
                With cmd
                    .Parameters(0).Value = Me.dgvDigital.Rows(i).Cells(0).Value
                    .Parameters(1).Value = Me.dgvDigital.Rows(i).Cells(1).Value
                    .Parameters(2).Value = Me.dgvDigital.Rows(i).Cells(2).Value
                    .Parameters(3).Value = Me.dgvDigital.Rows(i).Cells(3).Value
                    .ExecuteNonQuery()
                End With
            Next
            MsgBox("Success Inserting")

1 个答案:

答案 0 :(得分:1)

我已经回答了我的问题。通过使用子查询在我的值中选择以获得我想要的输出。

 Connections.connect()
                InsertQuery = "INSERT INTO DigitalTB(PumpNo,NozzleNo,TankID,Opening,Ending,ReadingType,UserShift,Date,Time)" &
                " VALUES(@PumpNo1,@NozzleNo1,@TankID1,(SELECT TOP 1 Ending FROM DigitalTB WHERE PumpNo = @PumpNo1 AND NozzleNo = @NozzleNo1 AND TankID = @TankID1 ORDER BY Ending DESC),@Ending1,@ReadingType1,@UserShift1,@Date1,@Time1)"



            cmd = New SqlCommand
            With cmd
                .Connection = Connections.cn
                .CommandText = InsertQuery
                .Parameters.Add(New SqlParameter("@Ending1", SqlDbType.Decimal))
                .Parameters.Add(New SqlParameter("@PumpNo1", SqlDbType.NVarChar, 10))
                .Parameters.Add(New SqlParameter("@NozzleNo1", SqlDbType.NVarChar, 10))
                .Parameters.Add(New SqlParameter("@TankID1", SqlDbType.NVarChar, 100))

                '  .Parameters.Add(New SqlParameter("@Opening1", Ending))   ' This part is where i insert the top 1 from ending
                .Parameters.AddWithValue("@UserShift1", Interfacefrm.lblUserInterface.Text)
                .Parameters.AddWithValue("@ReadingType1", "Fuel")
                .Parameters.AddWithValue("@Date1", Date.Now.ToShortDateString) 'I save it as string
                .Parameters.AddWithValue("@Time1", Date.Now.ToShortTimeString) 'I save it as string

                For i As Integer = 0 To Me.dgvDigital.Rows.Count - 1
                    With cmd
                        .Parameters(0).Value = Me.dgvDigital.Rows(i).Cells(0).Value
                        .Parameters(1).Value = Me.dgvDigital.Rows(i).Cells(1).Value
                        .Parameters(2).Value = Me.dgvDigital.Rows(i).Cells(2).Value
                        .Parameters(3).Value = Me.dgvDigital.Rows(i).Cells(3).Value
                        .ExecuteNonQuery()
                    End With
                Next

            End With
            MsgBox("Success Inserting")
            Connections.disconnect()