0% found this document useful (0 votes)
13 views1 page

Koneksi Mysql VBNet

This document contains code for connecting to a MySQL database and inserting data into a teachers table. It defines a connection string, opens the connection on form load, and includes code to insert a new teacher record on a button click with error handling.

Uploaded by

yasirdean
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views1 page

Koneksi Mysql VBNet

This document contains code for connecting to a MySQL database and inserting data into a teachers table. It defines a connection string, opens the connection on form load, and includes code to insert a new teacher record on a button click with error handling.

Uploaded by

yasirdean
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Imports MySql.Data.

MySqlClient
Public Class Form2
Public strconn As String = "server=localhost; uid=root; pwd=; database=gaji_guru;"
'buat objek adapter
Dim myadp As MySqlDataAdapter
'buat data tabel (agar data disusun tabel)
Dim dt As New DataTable
'buat perintah query disini
Public conn As New MySqlConnection(strconn)
Private Sub Koneksi()
'jika koneksi tertutup
If conn.State = ConnectionState.Closed Then
Try
'buka koneksi
conn.Open()
'tangkap error mysql, jika ada ( ini hanya sebagian)
Catch mex As MySqlException
If mex.Number = 0 Then
MsgBox("Tidak bisa connect ke db", "no server")
ElseIf mex.Number = 1045 Then
MsgBox("Salah user/pass mysql", "akses ditolak")
Else
MsgBox(mex.Number & mex.Message)
End If
'tangkap error umum
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Call Koneksi()
End Sub
Private Sub Bersih()
txtKode.Text = ""
txtNama.Text = ""
cboJabatan.Text = ""
txtKode.Focus()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim mycmd As New MySqlCommand("insert into tbl_guru (kd_guru,nama,jabatan) values ('"
& txtKode.Text & "','" & txtNama.Text & "','" & cboJabatan.Text & "')", conn)
Try
If mycmd.ExecuteNonQuery() = 1 Then
MsgBox("insert data berhasil")
Bersih()
Exit Sub
End If
Catch ex As MySqlException
MsgBox("insert data gagal")
End Try
End Sub

End Class

You might also like