This VB.NET code connects to a MySQL database to perform CRUD operations on a member/user table. It opens a connection to the database, displays existing members in a datagrid, allows adding/editing members by populating textboxes from a selected member, and deletes a member by their ID number by executing a DELETE query after user confirmation. The code handles validation, updating the UI, and closing the database connection.
Download as DOCX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
17 views
Hapus Database MySQL VB 2010
This VB.NET code connects to a MySQL database to perform CRUD operations on a member/user table. It opens a connection to the database, displays existing members in a datagrid, allows adding/editing members by populating textboxes from a selected member, and deletes a member by their ID number by executing a DELETE query after user confirmation. The code handles validation, updating the UI, and closing the database connection.
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3
Hapus Database MySQL VB 2010
Imports System.Data.Odbc Public Class Form1 Dim Conn As OdbcConnection Dim da As OdbcDataAdapter Dim ds As DataSet Dim str As String Dim CMD As OdbcCommand Dim RD As OdbcDataReader
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load Call KondisiAwal() End Sub Sub Koneksi() str = "Driver={MySQL ODBC 5.3 ANSI Driver};database=DBperpustakaan;server=localhost;uid=root" Conn = New OdbcConnection(str) If Conn.State = ConnectionState.Closed Then Conn.Open() End If End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or TextBox4.Text = "" Then MsgBox("Data belum lengkap, Pastikan Nomor anggota 6 Digit dan Semua form terisi") Exit Sub Else Call Koneksi()
If MessageBox.Show("Yakin akan dihapus..?", "",
MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then Dim hapus As String = "DELETE FROM anggota WHERE anggota.nomoragt = '" & TextBox1.Text & "'" CMD = New OdbcCommand(hapus, Conn) CMD.ExecuteNonQuery() MsgBox("Data berhasil di Hapus", MsgBoxStyle.Information, "Information") Call KondisiAwal() End If End If End Sub Sub KondisiAwal() TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" Koneksi() da = New OdbcDataAdapter("Select * from anggota", Conn) ds = New DataSet ds.Clear() da.Fill(ds, "anggota") DataGridView1.DataSource = (ds.Tables("anggota")) End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = Chr(13) Then Call Koneksi() CMD = New OdbcCommand("Select * from anggota where nomoragt='" & TextBox1.Text & "'", Conn) RD = CMD.ExecuteReader RD.Read()
If Not RD.HasRows Then
TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox2.Focus() Else TextBox2.Text = RD.Item("Namaagt") TextBox3.Text = RD.Item("alamatagt") TextBox4.Text = RD.Item("teleponagt") TextBox2.Focus() End If End If End Sub End Class