Koneksi praktis dengan program VB.Net

banyak sekali macam-macam kode untuk menghubungkan program database dengan kelompok dari visual basic, ini adalah salah satu contoh kode vb.net. copy paste kode ini:

Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Windows.Forms
Imports System.Resources

Public Class MainClass
Shared Sub Main()
‘Declare variables and objects
Dim strConnectionString As String = _
“Provider=Microsoft.Jet.OLEDB.4.0;” & _
“Data Source=Employee.mdb;”
Dim objConnection As New OleDbConnection(strConnectionString)
Dim strSQL As String = _
“SELECT FirstName, LastName FROM Employee”
Dim objCommand As New OleDbCommand(strSQL, objConnection)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New Data.DataTable(“Employee”)
Dim objDataRow As DataRow

Try
‘membuka koneksi ke database
objConnection.Open()

‘mengisi kedalam tabel data
objDataAdapter.Fill(objDataTable)

‘muatan form dalam listbox
For Each objDataRow In objDataTable.Rows
Console.WriteLine(objDataRow.Item(“FirstName”) & ” ” & _
objDataRow.Item(“LastName”))
Next
Catch OleDbExceptionErr As OleDbException
‘menulis exception
Console.WriteLine(OleDbExceptionErr.Message)
Catch InvalidOperationExceptionErr As InvalidOperationException
‘menulis the exception
Console.WriteLine(InvalidOperationExceptionErr.Message)
End Try

‘keluar dari koneksi
objConnection.Close()

‘membersihkan
objDataRow = Nothing
objDataTable.Dispose()
objDataTable = Nothing
objDataAdapter.Dispose()
objDataAdapter = Nothing
objCommand.Dispose()
objCommand = Nothing
objConnection.Dispose()
objConnection = Nothing

End Sub
End Class

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: