21 Mei 2011

Contoh Koneksi DataBase



1. Pembuatan Form1




     
     
     
    Public Class Form1

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

            If Tampilkan.HasRows = True Then
                While Tampilkan.Read()
                    If IsDBNull(Tampilkan("kdjur")) Then
                        TextBox2.Text = "_"
                    Else
                        TextBox2.Text = Tampilkan("nmjur")

                    End If
                End While
            End If

        End Sub

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Call Jurusan()
        End Sub

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

        End Sub
    End Class





     2.Pembuatan Form2



     


     
    Public Class Form2

        Sub Kosong() ' Untuk menghapus
            TextBox1.Text = ""
            TextBox2.Text = ""
            TextBox1.Focus()
        End Sub
        Sub Netral()
            BtTambah.Text = "&Tambah"
            BtUbah.Text = "&Ubah"
            BtHapus.Text = "&Hapus"
            BtKeluar.Text = "&Keluar"
            BtTambah.Enabled = True
            BtUbah.Enabled = True
            BtHapus.Enabled = True
            TextBox1.Enabled = True
        End Sub
        Sub cekrecord()
            Call Koneksi()
            Tampil.Connection = Database
            Tampil.CommandType = CommandType.Text
            Tampil.CommandText = "select * from jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
            'Fungsi Trim untuk menghilangkan spasi
            Tampilkan = Tampil.ExecuteReader

            If Tampilkan.HasRows = True Then 'untuk mengecek apakah ada record atau tdk
                hasilcek = True
            Else
                hasilcek = False

            End If
        End Sub

        Private Sub BtTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtTambah.Click
            Call simpankelompok()
        End Sub
        Sub simpankelompok()
            If BtTambah.Text = "&Tambah" Then
                TextBox1.Text = " "
                BtTambah.Text = "Simpan "
                BtUbah.Enabled = False 'enabled artinya tdk aktif
                BtHapus.Enabled = False
                BtKeluar.Text = "&Batal"'TextBox1.Enabled = False
                TextBox1.Focus()
            Else
                If TextBox1.Text = "" Then Exit Sub ' untuk mengecheck apakah data sudah diisi semua atau belum, semua harus terisi
                If TextBox2.Text = "" Then Exit Sub
                Call cekrecord() ' untuk mengechek data kode jurusan ada atau belum
                Try
                    If hasilcek = False Then ' check kalau data belum ada berarti simpan
                        Call Koneksi()
                        Tampil.Connection = Database
                        Tampil.CommandType = CommandType.Text
                        Tampil.CommandText = " Insert into Jurusan ( kdjur, nmjur )values('" & Trim(TextBox1.Text) & "','" & TextBox2.Text & "')"
                        Tampil.ExecuteNonQuery()
                        Call Kosong() ' memanggil untuk clear text box 1 &2
                    Else
                        MsgBox("Data Sudah Ada, Silakan Check Data Yang Diinput ", MsgBoxStyle.Critical, "Input Data")
                        TextBox1.Focus()

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

                End Try
            End If

        End Sub
        Private Sub BtKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtKeluar.Click
            If BtKeluar.Text = "&Keluar" Then
                Me.Close()
            Else
                Call Kosong()
                Call Netral()
            End If
        End Sub

        Private Sub BtUbah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUbah.Click
            Call Btubahkelompok()
        End Sub
        Sub Btubahkelompok()
            If BtUbah.Text = "&Ubah" Then
                TextBox1.Text = ""
                BtUbah.Text = "Simpan"
                BtTambah.Enabled = False
                Bthapus.Enabled = False
                BtKeluar.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 nmjur ='" & Trim(TextBox2.Text) & "' where kdjur='" & Trim(TextBox1.Text) & "'"
                    Tampil.ExecuteNonQuery()


                    Call Kosong()
                    TextBox1.Focus()

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

                End Try

            End If
        End Sub
        Private Sub BtHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtHapus.Click
            Call BtHapuskelompok()
        End Sub

        Sub BtHapuskelompok()
            If BtHapus.Text = "&Hapus" Then
                TextBox1.Text = ""
                BtHapus.Text = "Ok"
                BtTambah.Enabled = False
                BtUbah.Enabled = False
                BtKeluar.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 kdjur='" & 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 BtTambah.Text = "Simpan" Then
                    Call cekrecord()
                    If hasilcek = False Then
                        TextBox2.Focus()
                    Else
                        MsgBox("Kode Sudah Ada, Silakan ganti kode lain", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Informasi")
                        TextBox1.Focus()
                    End If
                Else
                    Call Koneksi()
                    Tampil.Connection = Database
                    Tampil.CommandType = CommandType.Text
                    Tampil.CommandText = "select * from jurusan where kdjur ='" & Trim(TextBox1.Text) & "'"
                    Tampilkan = Tampil.ExecuteReader

                    If Tampilkan.HasRows = True Then

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


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

        End Sub

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

        End Sub
    End Class




    3.Pembuatan Koneksi DataBase



    Imports System.Data
    Imports System.Data.Sql

    Module Koneksidatabase
        Public Database As New OleDb.OleDbConnection
        'berfungsi untuk koneksi antar vb.net
        Public Tampil As New OleDb.OleDbCommand
        'berfungsi memberikan fasilitas untuk perintah2 query
        Public Tampil2 As New OleDb.OleDbCommand
        'berfungsi menyimpan data/hasil dari koneksi untuk sementara
        Public Tampilkan As OleDb.OleDbDataReader
        'berfungsi membuat variable untuk menampilakan sebuah data dalam grid
        Public Grid As New OleDb.OleDbDataAdapter
        Public Ds As New DataSet
        Public Table, hasilcek As String
        Public record As New BindingSource
        Public noo As Integer
        Public DMLSql As New OleDb.OleDbCommand
        Public Sub Koneksi()
            Try
                Database.Close()
                'Server
                'Database.ConnectionString = "Provider=SQLOLEDB.1;Password=;Persist Security Info = True; User ID=sa; Initial Catalog=TI4K;Data Source=192.168.1.102"
                'Lokal
                Database.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info = False; Initial Catalog=TI4K;Data Source=.;"
                Database.Open()
            Catch ex As Exception
                MsgBox("Koneksi belum berhasil")
            End Try
            MsgBox("Koneksi berhasil")
        End Sub
    End Module




    4.Login Form



    Public Class LoginForm1


        Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
            If UsernameTextBox.Text = "Jokosembung" And PasswordTextBox.Text = "Ojolali" Then
                MDIParent1.Show()
                Me.Visible = False
            Else
                MsgBox("login gagal, silakan ulangi lagi")
            End If
        End Sub

        Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
            Me.Close()
        End Sub

       
        Private Sub LogoPictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LogoPictureBox.Click

        End Sub
    End Class