Iklan
=====================================================
=====================================================
=====================================================
[x]
WELCOME TO JAVA ELECTRONIC Selamat Berkarya Nusa Dan Bangsa Bersatu Membangun Negri Tercinta


Connecting MySQL Database Dengan Visual Basic 2008 Mengunakan Class

Jumat, 04 Juli 20140 komentar

Dalam tutorial ini saya akan mengajarkan Anda bagaimana menggunakan kelas dalam menghubungkan database MySQL dan Visual Basic 2008. Apa kelas? Kelas A adalah sekelompok objek yang mendefinisikan fungsi sebagai seperangkat sifat dan metode yang akan merespon. Dan itu juga merupakan wadah data Anda dan kode. Dimana Anda dapat membatasi data yang Anda mengakses. Dalam Object-Oriented Programming (OOP), kelas, metode dan properti membantu Anda membuat aplikasi lengkap.

Mari kita mulai:
Langkah 1:


Create a Database.
  1. CREATE DATABASE `test` ;
Step2:
Buka  Visual Basic 2008, buat Project baru, dan nama Form beri nama sesui keinginan.
First Form
Step3:
Cari ke solusi explorer, klik kanan nama proyek Anda, klik add dan tambahkan Class dan Beri nama“connection”.
Solution Explorer
Step4:
Masukan Code class(connection), kode berikut untuk menyambungkaan MySQL Database.
  1. Imports MySql.Data.MySqlClient
  2. Public Class connection
  3. 'set a string connection
  4. Private strCon As String = "server=localhost;user id=root;database=test"
  5.  
  6. 'set a connection
  7. Private Function Mycon() As MySqlConnection
  8. Return New MySqlConnection(strCon)
  9. End Function
  10. 'pass the value of Mycon to Connection
  11. Private Connection As MySqlConnection = Mycon()
  12. 'set a property of connection
  13. Property con() As MySqlConnection
  14. Get
  15. 'return the connection
  16. Return Connection
  17. End Get
  18. Set(ByVal value As MySqlConnection)
  19. 'set the connection
  20. Connection = value
  21. End Set
  22. End Property
  23. End Class
Discriptions: Properti memiliki fungsi yang dapat read-only, write-only dan read-write. GET accessor menyediakan untuk membaca akses dan Set accessor menyediakan untuk menulis akses.
Step5:
Kembali ke tampilan desain, double klik tombol dan melakukan kode berikut.
  1. Public Class Form1
  2. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  3. 'call a class and set a new class of connection
  4. Dim Mycon As connection = New connection
  5.  
  6. If Button1.Text = "connect" Then 'checking the text of a button
  7.  
  8. 'openning the connection
  9. Mycon.con.Open()
  10. If Mycon.con.State = ConnectionState.Open Then 'check if the connection is open or not.
  11. 'result
  12. Button1.Text = "disconnect" 'change the text of the button
  13. Label1.Text = "Connected" 'change the text of the label
  14. Else
  15. Button1.Text = "connect" 'change the text of the button
  16. Label1.Text = "Disconnected"
  17. End If
  18. Else
  19. 'clossing the connection
  20. Mycon.con.Close()
  21. If Mycon.con.State = ConnectionState.Open Then
  22. Button1.Text = "disconnect" 'change the text of the button
  23. Label1.Text = "Connected" 'change the text of the label
  24. Else
  25. Button1.Text = "connect" 'change the text of the button
  26. Label1.Text = "Disconnected" 'change the text of the label
  27. End If
  28. End If
  29. End Sub
  30. End Class
 Salam Hangat Java-Electronic
Share this article :

Posting Komentar

 
Support : Joko-Electronic | Hangat-News | Joko Sugiono
Copyright © 2012. Java-Electronic - All Rights Reserved
Published by Java-Electronic
Proudly powered by Blogger