Cara untuk melakukan koneksi ke database access 2003 adalah sebagai berikut:
Imports System.Data.OleDb
Public Class Form1
Mendefinisikan variabel - varabel untuk melakukan koneksi ke database dan query
Dim CONN As OleDbConnection
Dim DA As OleDbDataAdapter
Dim DS As New DataSet
Dim CMD As OleDbCommand
Dim DR As OleDbDataReader
Sub Koneksi()
Try
String koneksi ke database access 2003
CONN = New OleDbConnection ("provider=microsoft.jet.oledb.4.0; data source=DBAccess2003.mdb")
CONN.Open()
MsgBox("koneksi sukses")
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Menampilkan data ke datagrid
Call Koneksi()
DA = New OleDbDataAdapter("SELECT * FROM TBLPEGAWAI", CONN)
DS = New DataSet
DA.Fill(DS)
DataGridView1.DataSource = DS.Tables(0)
DataGridView1.ReadOnly = True
Menampilkan data ke listbox
CMD = New OleDbCommand("SELECT * FROM TBLPEGAWAI", CONN)
DR = CMD.ExecuteReader
Do While DR.Read
ListBox1.Items.Add(DR.Item("NAMA"))
Loop
End Sub
End Class
Catatan : database access biasanya disimpan di Bin>Debug project yang bersangkutan.
Tidak ada komentar:
Posting Komentar