SKRIP INPUT, EDIT dan DELETE dengan VB NET + MYSQL - CORETAN TEKAJE

SKRIP INPUT, EDIT dan DELETE dengan VB NET + MYSQL

Share This

kali ini saya akan memberikan informasi bagaimana cara membuat CRUD dengan VB Net

1. Koneksi
Imports MySql.Data.MySqlClient
Module koneksi

    Public conn As MySqlConnection
    Sub konek()
        Try
            Dim str As String = "server=localhost;user id=root;password=;database=smkn22"
            conn = New MySqlConnection(str)
            If conn.State = ConnectionState.Closed Then
                conn.Open()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

2. Insert
Call konek()
        Dim cmd As MySqlCommand
        cmd = New MySqlCommand("INSERT into siswa values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "')", conn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("SUKSES INPUT DATA")

3. Update
 Call konek()
        Dim cmd As MySqlCommand
        cmd = New MySqlCommand("UPDATE siswa set nama='" & TextBox2.Text & "',kelas='" & TextBox3.Text & "',alamat='" & TextBox4.Text & "',telp='" & TextBox5.Text & "' where nis ='" & TextBox1.Text & "'", conn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("SUKSES EDIT DATA")

4. Delete
Call konek()
        Dim cmd As MySqlCommand
        cmd = New MySqlCommand("DELETE from siswa where nis='" & TextBox1.Text & "'", conn)
        cmd.ExecuteNonQuery()
        MessageBox.Show("SUKSES HAPUS DATA")


Tugas :

Buat table baru namanya adalah guru_nis
field disesuaikan
buat form guru seperti dibawah ini :


Fungsikan button simpan, delete dan update agar ke database.

Nilai ++ jika fieldnya kosong maka ada errorprovider, dan jika NIP sudah ada maka ada messagebox "NIP Sudah terdaftar, silahkan coba lagi"

Waktu pengerjaan sampai besok


UPDATE DATAGRID

Sub tampilSiswa()
        Call konek()
        Dim da As MySqlDataAdapter
        Dim ds As DataSet
        da = New MySqlDataAdapter("SELECT * FROM siswa", conn)
        ds = New DataSet
        da.Fill(ds, "siswa")
        DataGridView1.DataSource = ds.Tables("siswa")
    End Sub

Nanti tinggal di form loadnya di panggil 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        tampilSiswa()
 End Sub




Tidak ada komentar:

Posting Komentar

POST BOTTOM AD

Responsive Ads Here

Pages