我正在使用VB.net将配方写入mysql数据库。 插入食谱后,我需要返回该行的自动递增ID号。 (自动递增的ID列名称为RecipeID)
我尝试过此问题的不同变体: Return Last ID (IDENTITY) On Insert row VB.NET MySQL
,但不断出现未处理的异常错误。
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim mytimestamp As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=Splash123;database=awsbdata"
Dim reader As MySqlDataReader
Dim jobnumber As String = EthernetIPforCLXCom1.Read("BC_BatchCode")
Dim recipename As String = EthernetIPforCLXCom1.Read("Recipe_library[0].Name")
Dim recipeid As String = EthernetIPforCLXCom1.Read("Recipe_library[0].Number")
Dim perator As String = EthernetIPforCLXCom1.Read("BC_CurrentUser")
Dim slurrypress As Decimal = EthernetIPforCLXCom1.Read("PT1343_Imp")
Dim g1airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1022_Imp")
Dim g2airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1023_Imp")
Dim g3airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1024_Imp")
Dim g4airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1025_Imp")
Dim airflowcfm As Decimal = EthernetIPforCLXCom1.Read("FIT1042_Imp")
Dim concentration As Decimal = EthernetIPforCLXCom1.Read("MC_AbrasiveConcentation")
Dim tanklevel As Decimal = EthernetIPforCLXCom1.Read("LT1345_Imp")
Try
MysqlConn.Open()
Dim query As String
query = "insert into awsbdata.batchdata (RunTime,JobNumber,RecipeID,RecipeName,Operator,Concentration,G1AirPressure,G2AirPressure,G3AirPressure,G4AirPressure,AirFlow_CFM,SlurryPressure,TankLevel) values ('" & mytimestamp & "','" & jobnumber & "','" & recipeid & "','" & recipename & "','" & perator & "','" & concentration & "','" & g1airpressure & "','" & g2airpressure & "','" & g3airpressure & "','" & g4airpressure & "','" & airflowcfm & "','" & slurrypress & "','" & tanklevel & "')"
COMMAND = New MySqlCommand(query, MysqlConn)
reader = COMMAND.ExecuteReader
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub
我收到未处理的异常或连接未打开错误
答案 0 :(得分:0)
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Dim mytimestamp As String = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
MysqlConn = New MySqlConnection
MysqlConn.ConnectionString = "server=localhost;userid=root;password=Splash123;database=awsbdata"
Dim reader As MySqlDataReader
Dim jobnumber As String = EthernetIPforCLXCom1.Read("BC_BatchCode")
Dim recipename As String = EthernetIPforCLXCom1.Read("Recipe_library[0].Name")
Dim recipeid As String = EthernetIPforCLXCom1.Read("Recipe_library[0].Number")
Dim perator As String = EthernetIPforCLXCom1.Read("BC_CurrentUser")
Dim slurrypress As Decimal = EthernetIPforCLXCom1.Read("PT1343_Imp")
Dim g1airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1022_Imp")
Dim g2airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1023_Imp")
Dim g3airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1024_Imp")
Dim g4airpressure As Decimal = EthernetIPforCLXCom1.Read("PIT1025_Imp")
Dim airflowcfm As Decimal = EthernetIPforCLXCom1.Read("FIT1042_Imp")
Dim concentration As Decimal = EthernetIPforCLXCom1.Read("MC_AbrasiveConcentation")
Dim tanklevel As Decimal = EthernetIPforCLXCom1.Read("LT1345_Imp")
Try
MysqlConn.Open()
Dim query As String
query = "insert into awsbdata.batchdata (RunTime,JobNumber,RecipeID,RecipeName,Operator,Concentration,G1AirPressure,G2AirPressure,G3AirPressure,G4AirPressure,AirFlow_CFM,SlurryPressure,TankLevel) values ('" & mytimestamp & "','" & jobnumber & "','" & recipeid & "','" & recipename & "','" & perator & "','" & concentration & "','" & g1airpressure & "','" & g2airpressure & "','" & g3airpressure & "','" & g4airpressure & "','" & airflowcfm & "','" & slurrypress & "','" & tanklevel & "')"
COMMAND = New MySqlCommand(query, MysqlConn)
reader = COMMAND.ExecuteReader
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
Try
MysqlConn.Open()
COMMAND.CommandText = "SELECT Last_insert_id()"
Dim lastID = COMMAND.ExecuteScalar()
MsgBox(lastID)
MysqlConn.Close()
Catch ex As MySqlException
MessageBox.Show(ex.Message)
Finally
MysqlConn.Dispose()
End Try
End Sub