Personal Mind of ASEP

Sepercik Rasa yang Tercurah

Koneksi VB.NET ke MySql di vb.Net

‘ letakkan di bagian paling atas
Imports MySql.Data.MySqlClient

‘code
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
‘ Memeriksa apakah objek koneksi masih ada
‘ Jika ada, maka menutup terlebih dahulu
If Not Conn Is Nothing Then Conn.Close()
‘ memasukkan informasi ke variabel
ConnString = “server=localhost;” _
& “uid=tidak_ada;” _
& “pwd=apasaja;” _
& “database=test;”
Try
‘ Membuat objek MySqlConnection melalui
‘ konstruktor dengan parameter string koneksi
Conn = New MySqlConnection(ConnString)
‘ membuka koneksi database
Conn.Open()
MsgBox(“Koneksi ke ” & Conn.DataSource _
& Chr(10) & “Status : ” & Conn.State.ToString(), _
MsgBoxStyle.Information, “Sukses”)
‘ menutup koneksi
Conn.Close()
Catch ex As MySqlException
Select Case ex.Number
Case 0
MessageBox.Show(“Tidak dapat Koneksi ke ” _
& Conn.DataSource, “Koneksi Gagal”, _
MessageBoxButtons.OK, _
MessageBoxIcon.Error)
Case 1045
MessageBox.Show(“Nama user atau password ” & _
” tidak dikenal”, “Login Gagal”, _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Select
Finally
‘ menghapus objek koneksi
Conn.Dispose()
End Try

End Sub ‘ end of Sub Button1_Click

September 26, 2005 - Posted by | Uncategorized

No comments yet.

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: