如何使用vb.net在mysql数据库中查找每台机器id的最后一行?
像这样的东西应该有用如何使用vb.net在mysql数据库中查找每台机器id的最后一行?,mysql,vb.net,Mysql,Vb.net,像这样的东西应该有用 Private Sub btnAddSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSchedule.Click Try Dim ts As TimeSpan = TimeSpan.Parse(Me.dtpSchedule.Text ' PROBLEM IN HERE ??? D
Private Sub btnAddSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSchedule.Click
Try
Dim ts As TimeSpan = TimeSpan.Parse(Me.dtpSchedule.Text
' PROBLEM IN HERE ???
Dim rwFound As DataRow = MyDataset.Tables("totalizer_setting").Rows.Find( ??? )
If IsNothing(rwFound) Then
Dim rw As DataRow = MyDataset.Tables("totalizer_setting").NewRow
rw("machine_id") = Me.txtMachineID.Text
rw("time_schedule") = ts
rw("product_quantity") = DBNull.Value
MyDataset.Tables("totalizer_setting").Rows.Add(rw)
Dim sql As String = String.Format("INSERT INTO {0}.totalizer_setting (machine_id, time_schedule, product_quantity) VALUES ({1});", varSchema, "@machine_id, @time_schedule, @product_quantity")
Dim cmd As New MySqlCommand(sql, objCon)
cmd.Parameters.AddWithValue("@machine_id", Me.txtMachineID.Text)
cmd.Parameters.AddWithValue("@time_schedule", ts)
cmd.Parameters.AddWithValue("@product_quantity", DBNull.Value)
MyDataAdapter.InsertCommand = cmd
MyDataAdapter.Update(MyDataset, "totalizer_setting")
cmd.Dispose()
End If
Catch ex As MySqlException
MessageBox.Show("Error occurred in btnAddSchedule_Click Function" & vbCrLf & ex.Number & " – " & ex.Message)
End Try
End Sub
再想一想,我不知道mysql是否有last()
函数(sql server有)。如果mysql没有,您可以按相反顺序使用first()
,或者查看中的答案,以获得更有效的解决方案
Private Sub btnAddSchedule_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSchedule.Click
Try
Dim ts As TimeSpan = TimeSpan.Parse(Me.dtpSchedule.Text
' PROBLEM IN HERE ???
Dim rwFound As DataRow = MyDataset.Tables("totalizer_setting").Rows.Find( ??? )
If IsNothing(rwFound) Then
Dim rw As DataRow = MyDataset.Tables("totalizer_setting").NewRow
rw("machine_id") = Me.txtMachineID.Text
rw("time_schedule") = ts
rw("product_quantity") = DBNull.Value
MyDataset.Tables("totalizer_setting").Rows.Add(rw)
Dim sql As String = String.Format("INSERT INTO {0}.totalizer_setting (machine_id, time_schedule, product_quantity) VALUES ({1});", varSchema, "@machine_id, @time_schedule, @product_quantity")
Dim cmd As New MySqlCommand(sql, objCon)
cmd.Parameters.AddWithValue("@machine_id", Me.txtMachineID.Text)
cmd.Parameters.AddWithValue("@time_schedule", ts)
cmd.Parameters.AddWithValue("@product_quantity", DBNull.Value)
MyDataAdapter.InsertCommand = cmd
MyDataAdapter.Update(MyDataset, "totalizer_setting")
cmd.Dispose()
End If
Catch ex As MySqlException
MessageBox.Show("Error occurred in btnAddSchedule_Click Function" & vbCrLf & ex.Number & " – " & ex.Message)
End Try
End Sub
select last(time_schedule), last(product_quality) from yourTable group by machine_id