Senin, 30 Mei 2011

Modul koneksi

Imports System.Data
Imports System.Data.Sql
Module koneksidatabase
Public Database As New OleDb.OleDbConnection
Public Tampil As New OleDb.OleDbCommand
Public Tampil2 As New OleDb.OleDbCommand
Public Tampilkan As OleDb.OleDbDataReader
Public hasiljur, jur, hasilcek As String

Public Sub Koneksi()
Try
Database.Close()
'server'
'Database.ConnectionString = "Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;"User ID=sasa;Initial Catalog=PTJayaKarta;Data Source=192.168.10.4

'lokal
Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TI4P;Data Source=."
Database.Open()
'Tampil.Connection = Database
MsgBox(" Koneksi Berhasil ")
Catch ex As Exception
MsgBox(ex.ToString)
End Try



End Sub
End Module




Form3


Public Class Form3
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
Button2.Enabled = True
Button3.Enabled = True
TextBox1.Enabled = True
End Sub

Sub cekrecord()
Call Koneksi()
Tampil.Connection = Database
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from Jurusan where Kd_Jurusan='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
hasilcek = True
Else
hasilcek = False
End If
End Sub
Private Sub BtTambah_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.Enabled = False
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Call cekrecord()
Try
If hasilcek = "False" Then
Call Koneksi()

Tampil.Connection = Database
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "Insert into Jurusan (Kd_Jurusan,Nama_Jurusan)values ('" & TextBox1.Text & "','" & TextBox2.Text & "')"
Tampil.ExecuteNonQuery()
Call Kosong()
Else
MsgBox("Data sudah ada, silah cek data yang di input", MsgBoxStyle.Critical, "Input data")
TextBox1.Focus()
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
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call Btubahkelompok()
End Sub
Sub Btubahkelompok()
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
If TextBox2.Text = "" Then Exit Sub
Try
Call Koneksi()

Tampil.Connection = Database
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "update Jurusan set Nama_Jurusan ='" & Trim(TextBox2.Text) & "' where Kd_Jurusan='" & Trim(TextBox1.Text) & "'"
Tampil.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 BtHapuskelompok()
End Sub
Sub BtHapuskelompok()
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 Koneksi()

Tampil.Connection = Database
Tampil.CommandType = CommandType.Text

Tampil.CommandText = "Delete from Jurusan where Kd_Jurusan='" & Trim(TextBox1.Text) & "'"
Tampil.ExecuteNonQuery()

Call Kosong()
TextBox1.Focus()

Catch ex As Exception
MsgBox(ex.ToString())

End Try

End If
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 cekrecord()
If hasilcek = False Then
TextBox2.Focus()
Else
MsgBox("Kode sudah ada, silahkan ganti kode lain", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Infromasi")
TextBox1.Focus()
End If
Else
Call Koneksi()
Tampil.Connection = Database
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from jurusan where Kd_Jurusan='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
While Tampilkan.Read()
If IsDBNull(Tampilkan("Kd_Jurusan")) Then
TextBox1.Focus()
Else
TextBox2.Text = Tampilkan("Nama_Jurusan")
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

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

End Sub


End Class



Modul
Imports System.Data
Imports System.Data.Sql
Module koneksidatabase
Public Database As New OleDb.OleDbConnection
Public Tampil As New OleDb.OleDbCommand
Public Tampil2 As New OleDb.OleDbCommand
Public Tampilkan As OleDb.OleDbDataReader
Public hasiljur, jur, hasilcek As String

Public Sub Koneksi()
Try
Database.Close()
'server'
'Database.ConnectionString = "Provider=SQLOLEDB.1;Password=123456;Persist Security Info=True;"User ID=sasa;Initial Catalog=PTJayaKarta;Data Source=192.168.10.4

'lokal
Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TI4P;Data Source=."
Database.Open()
'Tampil.Connection = Database
MsgBox(" Koneksi Berhasil ")
Catch ex As Exception
MsgBox(ex.ToString)
End Try

End Sub
End Module

0 komentar:

Posting Komentar

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Elf Coupons