Untuk menggunakan perintah insert, update dan delete ke database MySQL menggunakan pemrograman Visual Basic VB.NET, anda dapat menggunakan kode berikut, jika masih bingung Anda dapat melihat videonya agar lebih jelas.
Kode Koneksi:
Namespace accessdata
'Ini koneksi
Public Class koneksidata
Dim conect As New MySql.Data.MySqlClient.MySqlConnection("server=localhost;uid=root;pwd='';database='skyars'")
Public Function open() As MySql.Data.MySqlClient.MySqlConnection
Try
conect.Open()
Catch ex As Exception
MsgBox("Localhost belum aktif atau coba cek database !!", MsgBoxStyle.Information, "Notice !!")
End Try
Return conect
End Function
Public Function close() As MySql.Data.MySqlClient.MySqlConnection
Try
conect.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Return conect
End Function
End Class
End Namespace
Kode Form:
Imports MySql.Data.MySqlClient
Public Class Form1
#Region "deklarasi"
Dim mycmd As New MySqlCommand
Dim myconnection As New accessdata.koneksidata
#End Region
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
mycmd.Connection = myconnection.open
mycmd.CommandText = "insert into tbtes(nomor,nama) values('" & TNO.Text & "','" & TNAMA.Text & "')"
mycmd.ExecuteNonQuery()
myconnection.close()
MsgBox("Data berhasil disimpan !", MsgBoxStyle.Information, "Pemberitahuan!")
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
mycmd.Connection = myconnection.open
mycmd.CommandText = "update tbtes set nama='" & TNAMA.Text & "' where nomor='" & TNO.Text & "'"
mycmd.ExecuteNonQuery()
myconnection.close()
MsgBox("Data berhasil dirubah !", MsgBoxStyle.Information, "Pemberitahuan!")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
mycmd.Connection = myconnection.open
mycmd.CommandText = "delete from tbtes where nomor='" & TNO.Text & "'"
mycmd.ExecuteNonQuery()
myconnection.close()
MsgBox("Data berhasil dihapus !", MsgBoxStyle.Information, "Pemberitahuan!")
End Sub
End Class
0 komentar:
Posting Komentar
Kita bukanlah apa-apa tanpaNya, jika ada pertanyaan mengenai artikel silakan tinggalkan komentar anda melalui form komentar :)