Sebelumnya kita siapkan dulu database MySQL dengan PHP MyAdmin dengan nama database = mhs
nama tabel = biodata
nim varchar(10)
nama_mhs varchar(30)
alamat varchar(100)
jurusan varchar(4)
Download dahulu konektor mysql dengan vb.net di sini
Buka Ms Visual Studio –> VB.NET
Project –> Add Reference –> .NET pilih MySQL.Data –> Add
Buat tampilan seperti berikut:
Setting di properties sebagai berikut:
Komponen | Properties |
TextBox1 | Name : txtNIP |
TextBox2 | Name : txtNama |
TextBox3 | Name : txtAlamat |
Combobox1 | Name : cmbJurusan |
DataGridView1 | Name : DataGridView1 |
Komponen | Properties |
Button1 | Name : BSave Text : Save |
Button2 | Name : BHapus Text : Hapus |
Button3 | Name : BExit Text : Exit |
Buat di View Code:
Imports MySql.Data.MySqlClient
Public Class Form1
Dim kon As New MySqlConnection(“server=localhost ; user id=root; database=mhs”)
Dim perintah As New MySqlCommand
’emerer.com
Dim datatabel As New DataSet
Dim tampildata As New MySqlDataAdapter
Sub bersih()
txtNIM.Text = “”
txtNama.Text = “”
txtAlamat.Text = “”
cmbJurusan.Text = “”
txtNIM.Focus()
’emerer.com
End Sub
Sub tampilkan()
kon.Open()
perintah.Connection = kon
perintah.CommandType = CommandType.Text
perintah.CommandText = “select * from biodata”
tampildata.SelectCommand = perintah
’emerer.com
datatabel.Tables.Clear()
tampildata.Fill(datatabel, “biodata”)
DataGridView1.DataSource = datatabel.Tables(“biodata”)
kon.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.txtNIM.Focus()
Call tampilkan()
Me.Text = “DATA MAHASISWA STMIK PUTERA BATAM”
Me.BackColor = Color.Ivory
txtNIM.MaxLength = 9
’emerer.com
cmbJurusan.Items.Add(“SI”)
cmbJurusan.Items.Add(“TI”)
End Sub
Private Sub BSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BSave.Click
Try
kon.Open()
perintah.Connection = kon
perintah.CommandType = CommandType.Text
perintah.CommandText = “insert into biodata values (‘” & txtNIM.Text & “‘,'” & txtNama.Text & “‘,'” & txtAlamat.Text & “‘,'” & cmbJurusan.Text & “‘)”
perintah.ExecuteNonQuery()
kon.Close()
MsgBox(“Data sukses disimpan”, MsgBoxStyle.MsgBoxRight,Pesan”)
Call tampilkan()
Call bersih()
’emerer.com
Catch ex As Exception
MsgBox(“Data GAGAL disimpan, PERIKSA KONEKSI”, MsgBoxStyle.MsgBoxRight, “Pesan”)
End Try
End Sub
Private Sub BExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BExit.Click
Me.Close()
’emerer.com
End Sub
Private Sub BDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BDelete.Click
kon.Open()
perintah.Connection = kon
perintah.CommandType = CommandType.Text
perintah.CommandText = “delete from biodata where nama_mhs='” & txtNama.Text & “‘”
perintah.ExecuteNonQuery()
kon.Close()
MsgBox(“Data sukses di HAPUS “, MsgBoxStyle.Information, “Pesan”)
’emerer.com
bersih()
Call tampilkan()
BSave.Enabled = True
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
With DataGridView1.Rows.Item(i)
txtNIM.Text = .Cells(0).Value
txtNama.Text = .Cells(1).Value
txtAlamat.Text = .Cells(2).Value
cmbJurusan.Text = .Cells(3).Value
’emerer.com
End With
BSave.Enabled = False
End Sub
End Class