Membuat Aplikasi DataBase Dengan Menggunakan VB NET

Membuat Form (User Interface)
Tarik dua label dan 2 textbox, 2 RadioButton, 1 datagridview dan 6 command button dari toolbox ke form.Jika anda melakukannya dengan benar seharusnya  form nya seperti ini:
Aplikasi ini telah dites dan berjalan dengan mulus.
Sebagai bocoran saya pernah tes pada perusahaan software developer. Tes nya adalah membuat software seperti ini ;)Inilah kode lengkapnya:
Imports System.Data.SqlClient
Imports System.DataPublic Class frmKotaPrivate oConn As New SqlConnection
Private sConn As String = “Data Source=.\SQLEXPRESS;Initial Catalog=Dataku;Integrated Security=True”Private m_pos As Integer
Private oTbl As New DataTable

Private Sub cmdBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBrowse.Click
Dim oDS As New DataSet
Dim oDa As New SqlDataAdapter(“SELECT * FROM KOTA”, sConn)
oDa.Fill(oDS) oTbl = oDS.Tables(0)
DataGridView1.DataSource = oTbl
End Sub

Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdKeluar.Click
End
End Sub

Private Sub DataGridView1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.CurrentCellChanged

m_pos = BindingContext(oTbl).Position
txtKodeKota.Text = oTbl.Rows(m_pos).Item(0)
txtNamaKota.Text = oTbl.Rows(m_pos).Item(1)
End Sub

Private Sub cmdTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTambah.Click

Dim sql As String = “INSERT INTO KOTA VALUES (‘” & txtKodeKota.Text & “‘,'” & txtNamaKota.Text & “‘)”

Dim oCmd As New SqlCommand
oCmd.Connection = oConn
oCmd.CommandText = sql
oCmd.ExecuteNonQuery()
End Sub

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

oConn.ConnectionString = sConn
oConn.Open()

End Sub

Private Sub cmdCari_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCari.Click

Dim oDS As New DataSet
Dim oDa As New SqlDataAdapter(“SELECT * FROM KOTA WHERE KODE_KOTA='” & txtKodeKota.Text & “‘”, sConn)
oDa.Fill(oDS) oTbl = oDS.Tables(0)
DataGridView1.DataSource = oTbl

End Sub

Private Sub cmdHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdHapus.Click

Dim sql As String = “DELETE FROM KOTA WHERE KODE_KOTA='” & txtKodeKota.Text & “‘”

Dim oCmd As New SqlCommand
oCmd.Connection = oConn
oCmd.CommandText = sql
oCmd.ExecuteNonQuery()

End Sub

Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSimpan.Click

Dim sql As String = “UPDATE KOTA SET NAMA_KOTA='” & txtNamaKota.Text & “‘ WHERE KODE_KOTA='” & txtKodeKota.Text & “‘”

Dim oCmd As New SqlCommand
oCmd.Connection = oConn
oCmd.CommandText = sql
oCmd.ExecuteNonQuery()

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

Dim oDS As New DataSet
Dim oDa As New SqlDataAdapter(“SELECT * FROM KOTA ORDER BY KODE_KOTA ASC”, sConn)

oDa.Fill(oDS) oTbl = oDS.Tables(0)
DataGridView1.DataSource = oTbl

End Sub

Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged

Dim oDS As New DataSet
Dim oDa As New SqlDataAdapter(“SELECT * FROM KOTA ORDER BY NAMA_KOTA ASC”, sConn)

oDa.Fill(oDS) oTbl = oDS.Tables(0)
DataGridView1.DataSource = oTbl
End Sub

End Class

Ini adalah salah satu aksi software yang baru saja kita buat.

Catatan:

Jika Anda memakai database MS-Access ikuti petunjuk di bawah ini:

Imports System.Data.SqlClient -> Imports System.Data.OleDb

Private sConn As String = “Data Source=.\SQLEXPRESS;Initial Catalog=Dataku;Integrated Security=True”
->
Private sConn As String = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\dataku.mdb”

Dim oCmd As New SqlCommand -> Dim oCmd As New OleDbCommand

-> maksudnya ganti menjadi…

Kode ini sudah dicoba pada vbnet 2005, vbnet 2008 dan vbnet 2010 tidak ada masalah.

Untuk memperoleh penjelasan detail cara pembuatan form (User Interface) dan arti dari kode-kode di atas dengan lengkap, Anda bisa baca E-Book saya yang berjudul  Trik Database Visual Basic Net

About these ads

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Logout / Ubah )

Twitter picture

You are commenting using your Twitter account. Logout / Ubah )

Facebook photo

You are commenting using your Facebook account. Logout / Ubah )

Google+ photo

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

Ikuti

Get every new post delivered to your Inbox.

%d bloggers like this: