Membuat Konecksi Database

Sudah lama rasanya tidak posting lagi , you weisss  kita mulai untuk membuat database beserta koneksinya ,,,  langsung  kita menuju  TKP..


Dalam membuat program kita biasa menggunakan database. Sebagai koneksi kita menggunakan module yang dibuat untuk mengkoneksikan dengan database Membuat modul koneksi dengan database adalah sebagai berikut :

Imports System.Data
Imports System.Data.Sql
Module koneksi
Public database As New OleDb.OleDbConnection
Public tampil2 As New OleDb.OleDbCommand
Public tampilkan As OleDb.OleDbDataReader
Public hasilcek As String

Public Sub bukakoneksi()
database.Close()
Try
database.ConnectionString = "provider=sqloledb.1;integrated security=SSPI;persist security info=false; initial catalog=cobasql;data source=."
database.Open()
MessageBox.Show("Koneksi kedatabase berhasil")
Catch ex As Exception
MessageBox.Show("koneksi gagal")
End
End Try
End Sub
End Module

Kemudian membuat sintax seperti ini dalam form yang baru.
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = "select * from jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
tampilkan = tampil2.ExecuteReader

If tampilkan.HasRows = True Then
While tampilkan.Read()
If IsDBNull(tampilkan("kdjur")) Then
TextBox2.text = tampilkan("nmjur")
Else
TextBox2.Text = tampilkan("nmjur")
End If
End While
End If
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub
End Class

kemudian membuat tampilan formnya sebagai berikut ;



Dan dilanjutkan dengan membuat sintax selanjutnya pada form baru selanjutnya ;

Public Class Form2
Sub kosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub
Sub netral()
Button1.Text = "Tambah"
Button2.Text = "Ubah"
Button3.Text = "Hapus"
Button4.Text = "Keluar"
Button1.Enabled = True 'enable=funsi aktif/tdaknya sebuah objek
Button2.Enabled = True
Button3.Enabled = True
Button4.Enabled = True
End Sub
Sub celrecord()
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = "select * from jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
tampilkan = tampil2.ExecuteReader
If tampilkan.HasRows = True Then ''ada atau tidak recod yg dimaksukkan
hasilcek = True
Else
hasilcek = False
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call simpankelompok()
End Sub
Sub simpankelompok()
If Button1.Text = "Tambah" Then
TextBox1.Text = ""
Button1.Text = "Simpan"
Button2.Enabled = False
Button3.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub ''then exit sub = keluar dari sub class dgan syarat textbox harus diisi/pengecekan validasi
If TextBox2.Text = "" Then Exit Sub
Call celrecord()
Try
If hasilcek = "False" Then
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Insert into Jurusan (kdjur,nmjur)values ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
tampil2.ExecuteNonQuery()
Call kosong()
Else
MsgBox("Data sudah ada , silahkan cek data yang akan diinput ", MsgBoxStyle.Critical, "Input data") ''critical = ! dalam msgbox
End If
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If Button4.Text = "Keluar" Then
Me.Close()
Else
Call kosong()
Call netral()
End If
End Sub
Sub ubahkelompok()
If Button2.Text = "Ubah" Then
TextBox1.Text = ""
Button2.Text = "Simpan"
Button1.Enabled = False
Button3.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub ''then exit sub = keluar dari sub class dgan syarat textbox harus diisi/pengecekan validasi
If TextBox2.Text = "" Then Exit Sub
Try
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Update Jurusan set nmjur='" & TextBox2.Text & "'where kdjur='" & TextBox1.Text & "'"
tampil2.ExecuteNonQuery()
Call kosong()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call ubahkelompok()
End Sub
Sub hapuskelompok()
If Button3.Text = "Hapus" Then
TextBox1.Text = " "
Button3.Text = "Ok"
Button1.Enabled = False
Button2.Enabled = False
Button4.Text = "Batal"
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Try
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " Delete from Jurusan where kdjur='" & Trim(TextBox1.Text)&"'"
tampil2.ExecuteNonQuery()
Call kosong()
TextBox1.Focus()
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End If
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Call hapuskelompok()

End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
If Button1.Text = "Simpan" Then
Call celrecord()
If hasilcek = False Then
TextBox2.Focus()
Else
MsgBox("Kode Sudah ada, silahkan ganti kode lain", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Informasi")
tampilkan = tampil2.ExecuteReader
End If
Else
Call bukakoneksi()
tampil2.Connection = database
tampil2.CommandType = CommandType.Text
tampil2.CommandText = " select* from Jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
tampil2.ExecuteNonQuery()

If tampilkan.HasRows = True Then
While tampilkan.Read()
If (IsDBNull(tampilkan("kdjur"))) Then
TextBox1.Focus()
Else
TextBox2.Text = tampilkan("nmjur")
TextBox2.Focus()
End If
End While
Else
MsgBox("Data tidak diketemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah Data")
End If
End If
End If
End Sub

End Class

kemudian membuat tampilan formnya lagi sebagai berikut :

0 komentar:

Posting Komentar

Semangat Never Die

Diberdayakan oleh Blogger.

Cari Blog Ini