VB.Net Database Connections

Berikut ini koneksi visual basic .net ke beberapa database:


1. MS-Access 2007


"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=..\database1.accdb;Persist Security Info=False;"


2. MS-Access 2003


"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\database1.mdb"


3. Microsoft SQL Server 2005 Express

"Data Source=.\SQLEXPRESS;Initial Catalog=dbku;Integrated Security=True"

OK. Anggap saja kita punya project dengan satu buah form, datagrid view dan command button.

Di bawah ini adalah projectnya jika dijalankan:




Berikut adalah contoh kode visual basic .net akses data MS-Access 2007:
Imports System.Data.OleDb
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oTbl As DataTable
        Dim sConn As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
           "..\database1.accdb;Persist Security Info=False;"
        Dim oConn As New OleDbConnection(sConn)
        Dim oCmd As New OleDbCommand("select * from siswa", oConn)
        Dim oDS As New DataSet
        Dim oDa As New OleDbDataAdapter(oCmd)
        oDa.Fill(oDS)
        oTbl = oDS.Tables(0)
        DataGridView1.DataSource = oTbl
    End Sub
 
Di bawah ini adalah contoh kode visual basic net koneksi database dengan sql server 2005 express:
Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buton1.Click
        Dim sConn As String = "Data Source=.\SQLEXPRESS;Initial Catalog=dbku;Integrated Security=True"
        Dim oTbl As DataTable
        Dim oConn As New SqlConnection(sConn)
        Dim oCmd As New SqlCommand("select * from siswa", oConn)
        Dim oDS As New DataSet
        Dim oDa As New SqlDataAdapter(oCmd)
        oDa.Fill(oDS)
        oTbl = oDS.Tables(0)
        DataGridView1.DataSource = oTbl
    End Sub
End Class
 

0 komentar:

Poskan Komentar

Starcraft Pointer
 
Web Design by : Givo-Alfajri ..!! ®