Monday, March 16, 2015

cara koneksi vb.net dengan access 2007

Contoh coding untuk melakukan koneksi ke database access 2007 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 2007
CONN = New OleDbConnection("provider=microsoft.ace.oledb.12.0;data source=DBAccess2007.accdb")
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 TBLUser", CONN)
DS = New DataSet
DA.Fill(DS)
DataGridView1.DataSource = DS.Tables(0)
DataGridView1.ReadOnly = True

Menampilkan data ke listbox
CMD = New OleDbCommand("SELECT * FROM TBLUser", CONN)
DR = CMD.ExecuteReader
Do While DR.Read
ListBox1.Items.Add(DR.Item("NAMA_user"))
Loop
End Sub
End Class





EmoticonEmoticon