Kumpulan Tutorial Komputer dan Informasi Pendidikan - Ambrosius Haloho

Program koneksi, tambah data, edit data, delete data dan update data pada Visual Studio

Public Class Form1
    Dim tabel As OleDb.OleDbDataAdapter
    Dim data As DataSet
    Dim record As New BindingSource
    Sub bersih()
        txtharga.Text = ""
        txtid.Text = ""
        txtjlh.Text = ""
        txtkategori.Text = ""
        txtnama.Text = ""
        txttotal.Text = ""
        Call data_mahasiswa()
    End Sub
    Sub data_mahasiswa()
        Call dbkoneksi()
        tabel = New Data.OleDb.OleDbDataAdapter("select * from tbobat", database)
        data = New DataSet
        tabel.Fill(data)
        record.DataSource = data
        record.DataMember = data.Tables(0).ToString()
        DataGridView1.DataSource = record
    End Sub

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

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sqlquery As New OleDb.OleDbCommand
        sqlquery.Connection = database
        sqlquery.CommandType = CommandType.Text
        sqlquery.CommandText = "insert into tbobat values ('" & txtid.Text & "','" & txtkategori.Text & "','" & txtnama.Text & "','" & txtjlh.Text & "','" & txtharga.Text & "','" & txttotal.Text & "')"
        sqlquery.ExecuteNonQuery()
        MsgBox("Data terimpan")
        Call bersih()

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim sqlquery As New OleDb.OleDbCommand
        sqlquery.Connection = database
        sqlquery.CommandType = CommandType.Text
        sqlquery.CommandText = "Delete from tbobat where id = '" & txtid.Text & "'"
        sqlquery.ExecuteNonQuery()
        MsgBox("Data Dihapus")
        Call bersih()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            Call dbkoneksi()
            Dim sqlquery As New OleDb.OleDbCommand
            sqlquery.Connection = database
            sqlquery.CommandType = CommandType.Text
            sqlquery.CommandText = "Update tbobat set id = '" & txtid.Text & "',kategori = '" & txtkategori.Text & "', nama = '" & txtnama.Text & "',jumlah = '" & txtjlh.Text & "',harga = '" & txtharga.Text & "',total = '" & txttotal.Text & "' where id = '" & txtid.Text & "'"
            sqlquery.ExecuteNonQuery()
            MsgBox("Data Berhasil Di Update")
            Call bersih()
        Catch ex As Exception
            MsgBox(ex.ToString())
        End Try

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim id = InputBox("Masukkan Nim Mahasiswa")
        Try
            data.Tables(0).PrimaryKey = New DataColumn() {data.Tables(0).Columns("id")}
            Dim row As DataRow
            row = data.Tables(0).Rows.Find(id)

            txtkategori.Text = row("kategori")
            txtnama.Text = row("nama")
            txtid.Text = row("id")
            txtharga.Text = row("harga")
            txtjlh.Text = row("jumlah")
            txttotal.Text = row("total")
        Catch ex As Exception
            MsgBox("Data Tidak ada / anda batal mencari Data !!")
        End Try


    End Sub

    Private Sub txtharga_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtharga.TextChanged
        txttotal.Text = Val(txtjlh.Text) * Val(txtharga.Text)
    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        bersih()
    End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Me.Close()
    End Sub
End Class



Imports System.Data
Imports System.Data.Sql
Module koneksi
    Public database As OleDb.OleDbConnection
    Public Sub dbkoneksi()
        Try
            database = New OleDb.OleDbConnection("Provider=SQLOledB.1;Integrated Security=SSPI;Persist Security Info=False;Initial catalog=apotik;data source=ZULVAN-PC\SQLEXPRESS")
            database.Open()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
End Module



Share:

No comments:

Post a Comment

Silahkan tinggalkan komentar anda agar blogger ini bisa menjadi lebih baik :) By : Orang Usil Cari Uanga (ttd : Zulvan Sihaloho)

Google+ Followers

Sample Text

Semoga tutorial dan informasi yang saya bagikan melalui blog saya ini dapat bermanfaat bagi saudara sekalian :)