Jumat, 10 Juni 2011

operasi database .net

Private Sub tambahData()
        If ButtonInput.Text = "Input" Then
            ButtonInput.Text = "Simpan"
            Call gunaText(True)
            Call bersih()
            ButtonCari.Enabled = False
            ButtonEdit.Enabled = False
            ButtonHapus.Enabled = False
            ButtonExit.Enabled = False
            ButtonBatal.Enabled = True
        Else
            If TextBoxKodePetugas.Text = "" Or TextBoxNamaPetugas.Text = "" _
            Or TextBoxAlamat.Text = "" Then
                MsgBox("Data Tidak Boleh Kosong")
                Exit Sub
            End If
            Call cekrecord()
            Try
                If hasilcek = "False" Then
                    Call Konek()
                    Tampil.Connection = Database
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "insert into petugas values('" & TextBoxKodePetugas.Text & "','" & TextBoxNamaPetugas.Text & "','" & TextBoxAlamat.Text & "')"
                    Tampil.ExecuteNonQuery()
                    Call bersih()
                    Call tutupKoneksi()
                    MsgBox("penambahan sukses")
                Else
                    MsgBox("Data sudah ada, silahkan cek data yang diinput", MsgBoxStyle.Critical, "Input Data")
                    Call bersih()
                    TextBoxKodePetugas.Focus()
                End If
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End If
    End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub cekrecord()
        Call Konek()
        Tampil.Connection = Database
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from petugas where kdpetugas='" & Trim(TextBoxKodePetugas.Text) & "'"
        Tampilkan = Tampil.ExecuteReader
        If Tampilkan.HasRows = True Then
            hasilcek = True
        Else
            hasilcek = False
        End If
        Call tutupKoneksi()
    End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub penghapusan()
        If ButtonHapus.Text = "Hapus" Then
            ButtonHapus.Enabled = False
            ButtonInput.Enabled = False
            ButtonEdit.Enabled = False
            ButtonExit.Enabled = False
            ButtonBatal.Enabled = True
            MsgBox("Lakukan pencarian data yang ingin dihapus terlebih dahulu")
            TextBoxKodePetugas.Enabled = True
            Call bersih()
        Else
            If TextBoxKodePetugas.Text = "" Or TextBoxNamaPetugas.Text = "" _
            Or TextBoxAlamat.Text = "" Then
                MsgBox("Data Tidak Boleh Kosong")
                Exit Sub
            End If
            Try
                Call Konek()
                Tampil.Connection = Database
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "delete from petugas where kdpetugas='" & Trim(TextBoxKodePetugas.Text) & "'"
                Tampil.ExecuteNonQuery()
                Call bersih()
                MsgBox("Data Telah Dihapus")
                MsgBox("jika masih ingin melakukan penghapusan lakukan kembali pencarian")
                ButtonHapus.Enabled = False
                Call gunaText(False)
                TextBoxKodePetugas.Enabled = True
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End If
    End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------
Sub pencarian()
        Call Konek()
        Tampil.Connection = Database
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from petugas where kdpetugas='" & Trim(TextBoxKodePetugas.Text) & "'"
        Tampilkan = Tampil.ExecuteReader
        If Tampilkan.HasRows = True Then
            While Tampilkan.Read()
                If IsDBNull(Tampilkan("kdpetugas")) Then
                    TextBoxKodePetugas.Focus()
                Else
                    TextBoxNamaPetugas.Text = Tampilkan("nama")
                    TextBoxAlamat.Text = Tampilkan("alamat")
                End If
            End While
        Else
            MsgBox("Data tidak diketemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Peringatan")
        End If
    End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------
    Sub perubahan()
        If ButtonEdit.Text = "Edit" Then
            ButtonHapus.Enabled = False
            ButtonInput.Enabled = False
            ButtonEdit.Enabled = False
            ButtonExit.Enabled = False
            MsgBox("Lakukan pencarian data yang ingin diubah terlebih dahulu")
            TextBoxKodePetugas.Enabled = True
            ButtonBatal.Enabled = True
            Call bersih()
        Else
            If TextBoxKodePetugas.Text = "" Or TextBoxNamaPetugas.Text = "" _
            Or TextBoxAlamat.Text = "" Then
                MsgBox("Data Tidak Boleh Kosong")
                Call gunaText(False)
                ButtonHapus.Enabled = False
                ButtonInput.Enabled = False
                ButtonEdit.Enabled = False
                ButtonExit.Enabled = False
                MsgBox("Lakukan pencarian data yang ingin diubah terlebih dahulu")
                TextBoxKodePetugas.Enabled = True
                Exit Sub
            End If
            Try
                Call Konek()
                Tampil.Connection = Database
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update petugas set nama ='" & Trim(TextBoxNamaPetugas.Text) & "',alamat = '" & Trim(TextBoxAlamat.Text) & "' where kdpetugas='" & Trim(TextBoxKodePetugas.Text) & "'"
                Tampil.ExecuteNonQuery()
                Call bersih()
                ButtonEdit.Enabled = False
                MsgBox("jika masih ingin melakukan perubahan lakukan kembali pencarian")
                Call gunaText(False)
                TextBoxKodePetugas.Enabled = True
            Catch ex As Exception
                MsgBox(ex.ToString())
            End Try
        End If
    End Sub
----------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System.Data
Imports System.Data.Sql
Module koneksi
    Public Database As New OleDb.OleDbConnection
    Public Tampil As New OleDb.OleDbCommand
    Public Tampilkan As OleDb.OleDbDataReader
    Public Grid As New OleDb.OleDbDataAdapter
    Public Ds As New DataSet
    Public Tabel, hasilcek As String
    Public record As New BindingSource
    Public noo As Integer
    Public DMLSql As New OleDb.OleDbCommand
    Public Sub Konek()
        Try
            Database.Close()
            Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=tugas;Data Source=."
            Database.Open()
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub
    Public Sub tutupKoneksi()
        Database.Close()
    End Sub
End Module