Visual Studio 2012 – Akses VB.Net ke database MySQL

Agar aplikasi yang dibuat dengan vb.net bisa mengakses database MySQL, perlu menambahkan referece (add reference) pada project yang dibuat.

untuk contoh programnya bisa melihat code dibawah dengan :
3 buah textbox dan 1 buah button.

sebelumnya harus membuat database dengan table yang berisi field id,nama dan alamat.

Imports MySql.Data.MySqlClient

Public Class Form1
Dim myconn As New MySqlConnection(“Server=myserver;User Id=root;Password=;Database=DB01”)
Private Sub CmdTampilkan_Click(sender As Object, e As EventArgs) Handles CmdTampilkan.Click
If TxtID.Text = “” Then
MsgBox(“Masukkan ID”)
Else
Dim csql As New MySqlDataAdapter(“select * from karyawan where id = ‘” & TxtID.Text & “‘”, myconn)
Dim rst As New DataTable
csql.Fill(rst)
If rst.Rows.Count > 0 Then
TxtNama.Text = rst.Rows(0).Item(“nama”)
TxtAlamat.Text = rst.Rows(0).Item(“alamat”)
Else
MsgBox(“ID tidak ada”)
TxtID.Text = “”
TxtNama.Text = “”
TxtAlamat.Text = “”
End If
End If
End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Me.ReportViewer1.RefreshReport()
End Sub
End Class

Advertisements
Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in:

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: