Insert Data ke database MySQL VB.Net

alfa6661
- July 20, 2012
0 (0 votes)
Rate :
Cara Insert Data ke database MySQL VB.Net

Artikel ini akan membahas bagaimana mengisi (insert) data ke database MySQL dalam VB.Net dengan menggunakan modul koneksi yang telah di buat pada artikel sebelumnya.

Source Code

Tampil Data

Private Sub TampilData()
    Dim da As New MySqlDataAdapter("select * from mhs", KonekDb)
    Dim dt As New DataTable
    da.Fill(dt)
    DataGridView1.DataSource = dt
End Sub

Insert Data

Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
    Try
        Dim cmd As New MySqlCommand("insert into mhs values('" & txtNip.Text & "','" & txtNama.Text & "')", KonekDb)
        cmd.ExecuteNonQuery()
        TampilData()
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

Full Source

Imports MySql.Data.MySqlClient
Public Class Form1
 
    Private Sub btnInsert_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        Try
            Dim cmd As New MySqlCommand("insert into mhs values('" & txtNip.Text & "','" & txtNama.Text & "')", KonekDb)
            cmd.ExecuteNonQuery()
            TampilData()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
 
    Private Sub TampilData()
        Dim da As New MySqlDataAdapter("select * from mhs", KonekDb)
        Dim dt As New DataTable
        da.Fill(dt)
        DataGridView1.DataSource = dt
    End Sub
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TampilData()
    End Sub
End Class

Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter

– Eric S. Raymond

Have a better explanation?

If you have comments about this post. Please write your comments below

No comments associated with this article