Pada kali ini memaparkan tugas kelompok praktek pemograman sql. Dengan membuat aplikasi yang terkoneksi dengan database, yang telah dibuat sebelumnya menggunakan SQL SERVER 2014, memiliki isi tabel barang, supplier dan pembelian .
1. cara mengkoneksikan database dengan VB.NET dengan mengetikan seperti dibawah ini
Imports System.Data.SqlClient
Module koneksi
Public comSQL As New SqlClient.SqlCommand
Public Sql As String
Public conn As New SqlConnection
Public str As String = "Data Source = LENOVO
;initial catalog =201553072 ;trusted_connection = true"
Public Sub buka_koneksi()
If conn.State = ConnectionState.Closed Then
conn.ConnectionString = str
Try
conn.Open()
Catch ex As Exception
MsgBox("Koneksi gagal :"
& ex.ToString)
End Try
End If
End Sub
Public Sub tutup_koneksi()
If conn.State = ConnectionState.Open Then
Try
conn.Close()
Catch ex As Exception
MsgBox("gagal menutup koneksi :" & ex.ToString)
End Try
End If
End Sub
2. kemudian jika ingin membuat tampilan seperti dibawah
kita harus mengetikan seperti ini
Public Class form_menu
Private Sub
KeluarToolStripMenuItem_Click_1(sender As Object, e As EventArgs) Handles KeluarToolStripMenuItem.Click
Close()
End Sub
Private Sub
DataBarangToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles DataBarangToolStripMenuItem.Click
form_barang.MdiParent = Me
form_barang.Show()
form_supplier.Hide()
End Sub
Private Sub
DataSupplierToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
DataSupplierToolStripMenuItem.Click
form_supplier.MdiParent = Me
form_supplier.Show()
form_pembelian.Hide()
End Sub
Private Sub
DataPembelianToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles
DataPembelianToolStripMenuItem.Click
form_pembelian.MdiParent = Me
form_pembelian.Show()
form_barang.Hide()
End Sub
End Class
3. membuat form barang yang memiliki button / tombol save,edit,delete,close dan mengetikan seperti berikut
-
Save
Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
buka_koneksi()
Sql = "insert into barang values
('"
& kdbrg.Text & "', '" & nmbrg.Text & "', '" & jnsbrg.Text & "', '" & harga.Text & "', '" & jumlah.Text & "', '" & kadaluarsa.Text &
"')"
comSQL = New SqlCommand(Sql, conn)
Try
comSQL.ExecuteNonQuery()
MsgBox("Data Berhasil Disimpan")
Catch ex As Exception
MsgBox("Data Gagal Disimpan
!!!")
End Try
Call updatedata()
Call bersih()
tutup_koneksi()
End Sub
-
Edit
Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click
buka_koneksi()
Sql = "UPDATE barang SET
nmbrg='"
& nmbrg.Text & "', jnsbrg='" & jnsbrg.Text & "',
harga='"
& harga.Text & "', jumlah='" & jumlah.Text & "',
kadaluarsa='" & kadaluarsa.Text & "' WHERE kdbrg='" & kdbrg.Text & "'"
comSQL = New SqlCommand(Sql, conn)
Try
comSQL.ExecuteNonQuery()
MsgBox("Update berhasil")
kdbrg.Enabled = True
btnsave.Enabled = True
Catch ex As Exception
MsgBox("Update gagal !!")
End Try
Call updatedata()
Call bersih()
tutup_koneksi()
End Sub
-
Delete
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
Dim pesan As String
pesan = MsgBox("Apakah Anda Ingin Menghapus
Data ini ?", vbYesNo, "Konfirmasi")
If pesan = 6 Then
hapusbarang()
kdbrg.Enabled = True
btnsave.Enabled = True
End If
End Sub
-
Laporan
Private Sub
btnlap_Click(sender As Object, e As EventArgs) Handles
btnlap.Click
laporanbrg.Show()
End Sub
-
Close
Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
Close()
End Sub
contoh form barang
contoh hasil dari tombol laporan
4. membuat form supplier yang isi tombolnya sama seperti form barang
-
Edit
Imports System.Data.SqlClient
Public Class form_supplier
Sub updatedata()
buka_koneksi()
Sql = "select *from supplier"
Dim da As New SqlDataAdapter(Sql, conn)
Dim ds As New DataSet
da.Fill(ds)
Dim dt As New DataTable
For Each dt In ds.Tables
dgvsupplier.DataSource = dt
Next
tutup_koneksi()
End Sub
-
Simpan
Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
buka_koneksi()
Sql = "insert into supplier values
('"
& kdsup.Text & "', '" & nmsup.Text & "', '" & tgllhr.Text & "', '" & alamat.Text & "', '" & tlpkntr.Text & "', '" & cbjekel.Text & "', '" & email.Text & "', '" & txtjnsproduk.Text
& "')"
comSQL = New SqlCommand(Sql, conn)
Try
comSQL.ExecuteNonQuery()
MsgBox("Data Berhasil Disimpan")
Catch ex As Exception
MsgBox("Data Gagal Disimpan
!!!")
End Try
Call updatedata()
Call bersih()
tutup_koneksi()
End Sub
-
Hapus
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handlesbtndelete.Click
Dim pesan As String
pesan = MsgBox("Apakah Anda Ingin Menghapus
Data ini ?", vbYesNo, "Konfirmasi")
If pesan = 6 Then
hapussupplier()
kdsup.Enabled = True
btnsave.Enabled = True
End If
End Sub
-
Laporan
Private Sub btnlaporan_Click(sender As Object, e As EventArgs) Handles btnlaporan.Click
lapsupplier.Show()
End Sub
-
Close
Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
Close()
End Sub
contoh hasil form
form supplier
hasil dari tombol laporan
5. membuat form pembelian
-
Save
Private Sub btnsave_Click(sender As Object, e As EventArgs) Handles btnsave.Click
buka_koneksi()
Sql = "insert into pembelian values
('"
& nonota.Text & "', '" & kodebrg.Text & "', '" & kodesup.Text & "', '" & tglbeli.Text & "', '" & jumlahbeli.Text &
"', '" & hargabeli.Text & "', '" & total.Text & "')"
comSQL = New SqlCommand(Sql, conn)
Try
comSQL.ExecuteNonQuery()
MsgBox("Data Berhasil Disimpan")
Catch ex As Exception
MsgBox("Data Gagal Disimpan
!!!")
End Try
Call updatedata()
Call bersih()
tutup_koneksi()
End Sub
-
Edit
Private Sub btnedit_Click(sender As Object, e As EventArgs) Handles btnedit.Click
buka_koneksi()
Sql = "UPDATE supplier SET
kdbrg='"
& kodebrg.Text & "', kdsup='" & kodesup.Text & "',
tanggalbeli='" & tglbeli.Text & "', jumlahbeli='" & jumlahbeli.Text & "',
hargabeli='" & hargabeli.Text & "', total='" & total.Text & "' WHERE
nonota='"
& nonota.Text & "'"
comSQL = New SqlCommand(Sql, conn)
Try
comSQL.ExecuteNonQuery()
MsgBox("Update berhasil")
nonota.Enabled = True
btnsave.Enabled = True
Catch ex As Exception
MsgBox("Update gagal !!")
End Try
Call updatedata()
Call bersih()
tutup_koneksi()
End Sub
-
Delete
Private Sub btndelete_Click(sender As Object, e As EventArgs) Handles btndelete.Click
Dim pesan As String
pesan = MsgBox("Apakah Anda Ingin Menghapus
Data ini ?", vbYesNo, "Konfirmasi")
If pesan = 6 Then
hapuspembelian()
nonota.Enabled = True
btnsave.Enabled = True
End If
End Sub
-
Laporan
Private Sub btnlap_Click(sender As Object, e As EventArgs) Handles btnlap.Click
lappembelian.Show()
End Sub
-
Close
Private Sub btnclose_Click(sender As Object, e As EventArgs) Handles btnclose.Click
Close()
End Sub
form pembelian
hasil dari tombol laporan