获取添加到mysql数据库的记录的id
希望有人能帮我解决这个问题 我制作了一个链接到mysql数据库的应用程序。 我正在将远程主机的详细信息写入数据库。 我也在保存远程凭据,但保存在不同的表中。 我的“凭证”表中有一个名为“hosts\u linked\u id”的列,我想在“hosts”表中包含父记录的id 这是我到目前为止的代码获取添加到mysql数据库的记录的id,mysql,database,vb.net,vb.net-2010,Mysql,Database,Vb.net,Vb.net 2010,希望有人能帮我解决这个问题 我制作了一个链接到mysql数据库的应用程序。 我正在将远程主机的详细信息写入数据库。 我也在保存远程凭据,但保存在不同的表中。 我的“凭证”表中有一个名为“hosts\u linked\u id”的列,我想在“hosts”表中包含父记录的id 这是我到目前为止的代码 SQLConnection.ConnectionString = connectionstring Try If SQLConnection.State = Connec
SQLConnection.ConnectionString = connectionstring
Try
If SQLConnection.State = ConnectionState.Closed Then
SQLConnection.Open()
Dim SQLStatement As String = "INSERT INTO hosts(name, description, host, type, port) VALUES('" & txtname.Text & "','" & txtdescription.Text & "','" & txthost.Text & "','" & cmbtype.Text & "','" & txtport.Text & "')"
SaveData(SQLStatement)
SQLConnection.Open()
SQLStatement = "INSERT INTO credentials(hosts_linked_id, username, password, type) VALUES('" & txtname.Text & "','" & txtusername.Text & "','" & encryptedpassword & "','" & cmbtype.Text & "')"
SaveData(SQLStatement)
Else
SQLConnection.Close()
End If
此外,这里还有“SaveData”函数
Public Sub SaveData(ByRef SQLStatement As String)
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = SQLStatement
.CommandType = CommandType.Text
.Connection = SQLConnection
.ExecuteNonQuery()
End With
SQLConnection.Close()
MsgBox("Host has been added")
txtname.Text = ""
txtdescription.Text = ""
txthost.Text = ""
cmbtype.Text = ""
txtport.Text = ""
End Sub
我需要做的是获取第一个INSERT语句执行到变量中时创建的记录的id,以便在执行第二个INSERT语句时将其插入到“credentials”表中
我用谷歌搜索了一下,尝试了几种不同的方法,但都没有成功
有人能帮我指出正确的方向吗
提前谢谢
TL;DR:需要在执行insert语句时添加mysql记录的ID,并将其放入变量中可能会回答您的问题。确实如此!谢谢Tin Tran。我所要做的就是让我的第一个insert语句成为一个包含。。。选择LAST_INSERT_ID(),然后将其添加到savedata函数中。。。Dim cmd_result As Integer=CInt(cmd.ExecuteScalar())啊哈,现在我在每个表中添加了两条记录!!叹息