Kamis, 29 September 2011

Koneksi VB .Net ( VB 2003/VB 2005/VB 2008 ) dengan Berbagai Macam DataBase

Pastikan anda membuat module baru pada VB .Net dengan nama Module_koneksi Copy Script dibawah ini sesuai dengan Database yang anda gunakan

1. Koneksi VB .Net Menggunakan Menggunakan ODBC

Imports System.Data.ODbC

Module Module_koneksi

Dim connection As New OdbcConnection("DSN=NAMADSN;")

Private Sub connection_open()

Try

If connection.State <> ConnectionState.Closed Then connection.Close()

connection.Open()

Catch ex As Exception

End Try

End Sub

Private Sub connection_close()

Try

If connection.State <> ConnectionState.Open Then connection.Open()

connection.Close()

Catch ex As Exception

End Try

End Sub

End Module

2. Koneksi VB .Net Dengan Database SQL Server

Imports System.Data.SqlClient

Module Module_koneksi

Dim connection As New SqlConnection("server=NAMASERVER; user=NAMAUSER; integrated security=SSPI; initial catalog=NAMADATABASE")

Private Sub connection_open()

Try

If connection.State <> ConnectionState.Closed Then connection.Close()

connection.Open()

Catch ex As Exception

End Try

End Sub

Private Sub connection_close()

Try

If connection.State <> ConnectionState.Open Then connection.Open()

connection.Close()

Catch ex As Exception

End Try

End Sub

End Module

3. 3. Koneksi VB .Net Dengan Database Microsoft Accses

Simpan Database Accses anda ke Folder Bin/Debug/NAMADATABASE dalam Poject VB .Net

Imports System.Data.OleDb

Module Module_koneksi

Dim connection As New OleDbConnection("provider=Microsoft.jet.oledb.4.0;data source=" & Application.StartupPath & "\NAMADATABASE.mdb")

Private Sub connection_open()

Try

If connection.State <> ConnectionState.Closed Then connection.Close()

connection.Open()

Catch ex As Exception

End Try

End Sub

Private Sub connection_close()

Try

If connection.State <> ConnectionState.Open Then connection.Open()

connection.Close()

Catch ex As Exception

End Try

End Sub

End Module

4. 4. Koneksi VB .Net Dengan Database MySql

Untuk yang ini anda harus install ODBC MySql terlebih dahulu yang selanjutnya pada Project Anda Add Refernce lalu pilih ODBC MySql

Imports MySql.Data.MySqlClient

Module Module_koneksi

Dim connection As New MySqlClientConection("server=LOCALHOST; uid=UID; pwd=PASSWORD; database=NAMADATABASE")

Private Sub connection_open()

Try

If connection.State <> ConnectionState.Closed Then connection.Close()

connection.Open()

Catch ex As Exception

End Try

End Sub

Private Sub connection_close()

Try

If connection.State <> ConnectionState.Open Then connection.Open()

connection.Close()

Catch ex As Exception

End Try

End Sub

End Module

0 komentar:

Poskan Komentar