Jan 11, 2010

Select Insert Delete Data Into Mysql Table Dengan Vb.net

Karena saat ini anakberbakti lagi males nulis & mikir, maka tutorial vb.net mysql yang gampang-gampang dulu aja. Bahasan kita saat ini adalah bagaimana cara kita untuk menampilkan, insert dan menghapus data mysql pada datagirdview vb.net
Udah kawan, lsg aja ya berikut adalah petikan kode programnya (snipet)

Public Class Form1
    Dim cmd As New MySql.Data.MySqlClient.MySqlCommand
    Dim tambah As New MySql.Data.MySqlClient.MySqlCommand
    Dim hapusdata As New MySql.Data.MySqlClient.MySqlCommand
    Private Sub tampil()
        konek("localhost", "root", "", "menu")
        cmd.CommandText = "SELECT id,name FROM `menu`"
        cmd.Connection = conn
        Dim table As New DataTable
        Dim adapter As New MySql.Data.MySqlClient.MySqlDataAdapter(cmd.CommandText, conn)
        Dim cb As New MySql.Data.MySqlClient.MySqlCommandBuilder(adapter)
        adapter.Fill(table)
        DataGridView1.DataSource = table
    End Sub
    Private Sub hapus()
        If TextBox1.Text = "" Then
            MsgBox("Tentikan pilihan data yang akan dihapus", MsgBoxStyle.Information)
        Else
            hapusdata.CommandText = "delete from menu where name='" & TextBox1.Text & "'"
            hapusdata.Connection = conn
            Dim rsl As Integer = hapusdata.ExecuteNonQuery()
            If rsl > 0 Then
                MsgBox("data terhapus")
                tampil()
            End If

        End If

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

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        tambah.Connection = conn
        tambah.CommandText = "insert into menu (name) values ('" & TextBox1.Text & "')"
        Dim rsl As Integer = tambah.ExecuteNonQuery()
        If rsl > 0 Then
            MsgBox("data berhasil ditambahkan")
            tampil()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        hapus()
    End Sub

    Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value.ToString
    End Sub
End Class

dan berikut adalah hasil dari snipet code diatas














dan isa juga mendownload source code vb.net di menampilkanData.rar

Buat kawan yang mingkin agak bingung dengan tutorila diatas, mungkin kawan isa baca tutorial dasar yaitu koneksi mysql & vb.net di halaman http://anakberbakti.blogspot.com/2009/12/koneksi-vbnet-and-mysql.html

2 comments:

rey_rockstreet said...

. thanks bro atas tutor.ny
. kebetulan banget nih

. keep sharing ;)

edikst said...

bro untuk find data codingnya gmana ya.
thx b4