Rabu, 21 Desember 2016

Menampilkan Data dari Database menggunakan Fungsi ExecuteReader()

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
        Dim reader As SqlDataReader

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

        cnn = New SqlConnection(connectionString)
        sql = "SELECT * FROM Barang"
        Try
            cnn.Open()
            cmd = New SqlCommand(sql, cnn)
            reader = cmd.ExecuteReader()
            While reader.Read()
                MsgBox(reader.Item(0) & "  -  " & reader.Item(1) & "  -  " & reader.Item(2) & "  -  " & reader.Item(3))
            End While
            reader.Close()
            cmd.Dispose()
            cnn.Close()
        Catch ex As Exception
            MsgBox("Gagal.")
        End Try
    End Sub
End Class

Bila sukses maka akan muncul kotak dialog seperti gambar di bawah ini

0 komentar:

Posting Komentar

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