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
|
Tidak ada komentar:
Posting Komentar