Rabu, 21 Desember 2016

Simpan Edit Hapus pada VB.NET dengan Fungsi ExecuteNonQuery()

Buka project Koneksi String VB.NET 2010 From App.Config dan buat form seperti gambar di bawah ini
Ketikkan kode berikut :

Imports System.Data.SqlClient
Imports System.Configuration

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String
        Dim cnn As SqlConnection
        Dim cmd As SqlCommand
        Dim sql As String

        connectionString = ConfigurationManager.AppSettings("ConString").ToString()

        cnn = New SqlConnection(connectionString)
        sql = "insert into Barang (Id,Nama,Harga,Stok) values ('1','barang1','1000','1')"
        'sql = "update Barang set Nama='Ganti Nama' where Id = '1'"
        'sql = "delete Barang where Id='1'"
        Try
            cnn.Open()
            cmd = New SqlCommand(sql, cnn)
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            cnn.Close()
            MsgBox("Berhasil.")
        Catch ex As Exception
            MsgBox("Gagal.")
        End Try
    End Sub
End Class
Setelah selesai menambahkan kode diatas jalankan Project dengan klik tombol RUN
Catatan :
untuk update dan delete penulis kasih tanda komentar, untuk menggunakannya silahkan hapus komentarnya
'sql = "update Barang set Nama='Ganti Nama' where Id = '1'"
'sql = "delete Barang where Id='1'"

0 komentar:

Posting Komentar

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Justin Bieber, Gold Price in India