Mengkoneksikan SQL ke VisualStudio2010

Kisi UTS Sistem Basis Data



Imports System.Data
Imports System.Data.Sql
Module Module1
    Public database As OleDb.OleDbConnection
    Public Sub koneksi()
        Try
            database = New OleDb.OleDbConnection("Provider=SQLOledB.1;Integrated Security=SSPI;Persist Security Info=False;Initial catalog=dbmahasiswa;data source=WIN8-PC")
            database.Open()

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Module




Public Class form1
    Dim tabel As OleDb.OleDbDataAdapter
    Dim data As DataSet
    Dim record As New BindingSource
    Sub bersih()
        TxtNim.Text = ""
        TxtNama.Text = ""
        TxtKelas.Text = ""
        Call data_mahasiswa()
    End Sub
    Sub data_mahasiswa()
        Call koneksi()
        tabel = New Data.OleDb.OleDbDataAdapter("select * from tblmahasiswa", database)
        data = New DataSet
        tabel.Fill(data)
        record.DataSource = data
        record.DataMember = data.Tables(0).ToString()
        DataGridView1.DataSource = record
    End Sub
End Class


Private Sub form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      TxtNim.Enabled = False
      TxtNama.Enabled = False
      TxtKelas.Enabled = False 
Call data_mahasiswa()
    End Sub
End Class


Private Sub BtnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnTambah.Click
        TxtNim.Enabled = True
        TxtNama.Enabled = True
        TxtKelas.Enabled = True

        TxtNim.Text = ""
        TxtNama.Text = ""
        TxtKelas.Text = ""
        TxtNim.Focus()
    End Sub
End Class


Private Sub BtnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSimpan.Click
        Dim sqlquery As New OleDb.OleDbCommand
        sqlquery.Connection = database
        sqlquery.CommandType = CommandType.Text
        sqlquery.CommandText = "Insert into tblmahasiswa values ('" & TxtNim.Text & "','" &
        TxtNama.Text & "','" & TxtKelas.Text & "')"
        sqlquery.ExecuteNonQuery()
        MessageBox.Show("Data telah terimpan", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Call bersih()

        TxtNim.Enabled = False
        TxtNama.Enabled = False
        TxtKelas.Enabled = False
    End Sub
End Class


Private Sub BtnCari_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCari.Click
        Dim Nim_mahasiswa = InputBox("Masukkan Nim Mahasiswa/i")
        Try
            data.Tables(0).PrimaryKey = New DataColumn() {data.Tables(0).Columns("Nim")}
            Dim row As DataRow
            row = data.Tables(0).Rows.Find(Nim_mahasiswa)
            TxtNim.Text = row("Nim")
            TxtNama.Text = row("Nama")
            TxtKelas.Text = row("Kelas")

TxtNim.Enabled = False
            TxtNama.Enabled = True
            TxtKelas.Enabled = True

        Catch ex As Exception
            MessageBox.Show("Data yang anda cari tidak tersedia !! ", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
End Class


Private Sub BtnUbah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUbah.Click
        Try
            Call koneksi()
            Dim sqlquery As New OleDb.OleDbCommand
            sqlquery.Connection = database
            sqlquery.CommandType = CommandType.Text
            sqlquery.CommandText = "Update tblmahasiswa set Nama = '" & TxtNama.Text &
            "',Kelas = '" & TxtKelas.Text & "' where Nim = '" & TxtNim.Text & "'"
            sqlquery.ExecuteNonQuery()
            MessageBox.Show("Data berhasil diubah ", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Call bersih()

            TxtNim.Enabled = False
            TxtNama.Enabled = False
            TxtKelas.Enabled = False
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try
    End Sub
End Class



Dim hapus As String
        hapus = MessageBox.Show("Apakah anda yakin ingin menghapus data ini ?", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If hapus = MsgBoxResult.Yes Then
            Dim sqlquery As New OleDb.OleDbCommand
            sqlquery.Connection = database
            sqlquery.CommandType = CommandType.Text
            sqlquery.CommandText = "Delete from tblmahasiswa where Nim = '" &
            TxtNim.Text & "'"
            sqlquery.ExecuteNonQuery()
            MessageBox.Show("Data berhasil dihapus ", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Call bersih()

            TxtNim.Enabled = False
            TxtNama.Enabled = False
            TxtKelas.Enabled = False
        End If
    End Sub
End Class

Postingan terkait:

Belum ada tanggapan untuk "Mengkoneksikan SQL ke VisualStudio2010"

Post a Comment