Просто добавьте задачу этого подпрограммы в часть, где вы проверяете соединение. Так как соединение уже открыто, нет необходимости открывать его снова. Также не забудьте дать инструкцию Query .. (я полагаю, вставить)
Private Sub MetroButton1_Click(sender As Object, e As EventArgs) Handles admission_btn_save.Click
If System.IO.File.Exists("D:\DBSystem\Connection\connconfig.dbs") = True Then
Dim strFile As String = "D:\DBSystem\Connection\connconfig.dbs"
Dim sr As New IO.StreamReader(strFile)
Dim ip As String
Dim port As String
Dim userid As String
Dim password As String
Dim database As String
'reading
ip = sr.ReadLine()
port = sr.ReadLine()
userid = sr.ReadLine()
password = sr.ReadLine()
database = sr.ReadLine()
sr.Close()
'default connection code
Dim serverstring As String = "server=" & ip & ";port=" & port & ";database=" & database & ";user ID=" & userid & ";password=" & password & ";OldGuids=true"
Dim sqlconnection As MySqlConnection = New MySqlConnection
sqlconnection.ConnectionString = serverstring
Try
If sqlconnection.State = ConnectionState.Closed Then
sqlconnection.Open()
MsgBox("Successfully connected to database!")
Dim sqlstatement As String = "Insert int >>>Your Query"
Dim cmd As MySqlCommand = New MySqlCommand
With cmd
.CommandText = sqlstatement
.CommandType = CommandType.Text
.Connection = sqlconnection
.ExecuteNonQuery()
End With
sqlconnection.Close()
MsgBox("Successfully recorded!")
Else
sqlconnection.Close()
MsgBox("connection closed!")
End If
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'reading from notepad config file
Else
MessageBox.Show("Do not have any previous configurations!", "Error",
MessageBoxButtons.OK)
End If
End Sub