Rabu, 08 Juni 2011

Contoh Koneksi Database

1.Form Login

Source Codenya        :
Public Class login

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Call Koneksi()
        Try
            Tampil.Connection = Database
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "select * from PENGGUNA where NAMA='" & TextBox1.Text & "'and PWD='" & TextBox2.Text & "'"

            Tampilkan = Tampil.ExecuteReader

            If Tampilkan.HasRows = True Then
                menu_utama.Show()

                Me.Visible = False
            Else
                MsgBox("Nama dan Password yang Anda Masukkan Salah, Silahkan Ulangi Lagi")
            End If
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try



    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.Close()
    End Sub
    Sub bersih()
        TextBox1.Text = ""
        TextBox2.Text = ""
    End Sub


End Class

2. Form Menu Utama


Source Codenya        :
Public Class menu_utama

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

    End Sub

    Private Sub ObatToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ObatToolStripMenuItem.Click
        OBAT.Show()

    End Sub

    Private Sub PasienToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasienToolStripMenuItem.Click
        PASIEN.Show()

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()

    End Sub

    Private Sub PenjualanToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PenjualanToolStripMenuItem.Click
        PENJUALAN.Show()

    End Sub
End Class




3. Form Obat


Source Codenya        :
Public Class OBAT
    Sub kosong()
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
        TextBox4.Text = ""
        TextBox5.Text = ""
        TextBox6.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
        Button4.Enabled = True
        TextBox1.Enabled = True
    End Sub
    Sub cekrecord()
        Call Koneksi()
        Tampil.Connection = Database
        Tampil.CommandType = CommandType.Text
        Tampil.CommandText = "select * from OBAT where KD_OBAT='" & Trim(TextBox1.Text) & "'"
        Tampilkan = Tampil.ExecuteReader

        If Tampilkan.HasRows = True Then
            hasilcek = True
        Else
            hasilcek = False
        End If
    End Sub
    Sub tambah()
        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
            If TextBox3.Text = "" Then Exit Sub
            If TextBox4.Text = "" Then Exit Sub
            Call cekrecord()
            Try
                If hasilcek = "False" Then
                    Call Koneksi()

                    Tampil.Connection = Database
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "Insert into OBAT (KD_OBAT,NM_OBAT,JENIS,GOLONGAN,SATUAN,HARGA)values ('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "','" & TextBox5.Text & "','" & TextBox6.Text & "')"
                    Tampil.ExecuteNonQuery()


                    Call kosong()

                    TextBox1.Focus()

                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
    Sub ubah()

        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
            If TextBox3.Text = "" Then Exit Sub
            If TextBox4.Text = "" Then Exit Sub
            If TextBox5.Text = "" Then Exit Sub
            If TextBox6.Text = "" Then Exit Sub
            Call cekrecord()
            Try

                Call Koneksi()

                Tampil.Connection = Database
                Tampil.CommandType = CommandType.Text
                Tampil.CommandText = "update OBAT set NM_OBAT ='" & Trim(TextBox2.Text) & "', JENIS ='" & TextBox3.Text & "', GOLONGAN ='" & TextBox4.Text & "', SATUAN ='" & TextBox5.Text & "', HARGA ='" & TextBox6.Text & "' where KD_OBAT='" & Trim(TextBox1.Text) & "'"
                Tampil.ExecuteNonQuery()
                Call kosong()
                TextBox1.Focus()

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

            End Try

        End If
    End Sub
    Sub hapus()

        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
            If TextBox3.Text = "" Then Exit Sub
            If TextBox4.Text = "" Then Exit Sub
            If TextBox5.Text = "" Then Exit Sub
            If TextBox6.Text = "" Then Exit Sub

            Call cekrecord()
            Try
                Call Koneksi()

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

                Tampil.CommandText = "Delete from PASIEN where KD_PASIEN='" & 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
            Call cekrecord()
            Call Koneksi()
            Tampil.Connection = Database
            Tampil.CommandText = "select * from OBAT where KD_OBAT='" & Trim(TextBox1.Text) & "'"
            Tampilkan = Tampil.ExecuteReader

            If Tampilkan.HasRows = True Then
                While Tampilkan.Read()
                    If IsDBNull(Tampilkan("KD_OBAT")) Then
                        TextBox1.Focus()
                    Else
                        TextBox2.Text = Tampilkan("NM_OBAT")
                        TextBox3.Text = Tampilkan("JENIS")
                        TextBox4.Text = Tampilkan("GOLONGAN")
                        TextBox5.Text = Tampilkan("SATUAN")
                        TextBox6.Text = Tampilkan("HARGA")

                    End If
                End While
            Else
                MsgBox("data tidak ditemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah data")
            End If
        End If
    End Sub

    Private Sub PASIEN_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 tambah()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Call ubah()
   End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Call hapus()
    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 Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label9.Click

    End Sub

    Sub AHMAD()
  
  Call Koneksi()

        Tampil2.Connection = Database
        Tampil2.CommandType = CommandType.Text
        Tampil2.CommandText = "select * from OBAT where KD_OBAT ='" & Trim(TextBox1.Text) & "'"
     
  Tampilkan = Tampil2.ExecuteReader

        If Tampilkan.HasRows = True Then
            While Tampilkan.Read()
                If IsDBNull(Tampilkan("KD_OBAT")) Then
                    TextBox11.Text = " "
                Else
                    ListView1.Items.Add(Tampilkan("NM_OBAT") & " " & (Tampilkan("JENIS") & " " & (Tampilkan("GOLONGAN") & " " & (Tampilkan("SATUAN") & " " & (Tampilkan("HARGA"))))))
                End If
            End While
   End If
    End Sub
   Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Call AHMAD()
    End Sub
End Class


4. Source Code Module
Imports System.Data
Imports System.Data.Sql

Module Module1

    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()    
Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=UAS_AHMAD;Data Source=."
            Database.Open()
            'Tampil.Connection = Database
            MsgBox(" Koneksi Berhasil ")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
   End Sub
End Modul


0 komentar:

Posting Komentar