Mari kita mulai:
Langkah 1:
Create a Database.
Step2:
Buka Visual Basic 2008, buat Project baru, dan nama Form beri nama sesui keinginan.
Step3:
Cari ke solusi explorer, klik kanan nama proyek Anda, klik add dan tambahkan Class dan Beri nama“connection”.
Step4:
Masukan Code class(connection), kode berikut untuk menyambungkaan MySQL Database.
Imports MySql.Data.MySqlClient Public Class connection 'set a string connection Private strCon As String = "server=localhost;user id=root;database=test" 'set a connection Private Function Mycon() As MySqlConnection Return New MySqlConnection(strCon) End Function 'pass the value of Mycon to Connection Private Connection As MySqlConnection = Mycon() 'set a property of connection Property con() As MySqlConnection Get 'return the connection Return Connection End Get Set(ByVal value As MySqlConnection) 'set the connection Connection = value End Set End Property End Class
Step5:
Kembali ke tampilan desain, double klik tombol dan melakukan kode berikut.
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'call a class and set a new class of connection Dim Mycon As connection = New connection If Button1.Text = "connect" Then 'checking the text of a button 'openning the connection Mycon.con.Open() If Mycon.con.State = ConnectionState.Open Then 'check if the connection is open or not. 'result Button1.Text = "disconnect" 'change the text of the button Label1.Text = "Connected" 'change the text of the label Else Button1.Text = "connect" 'change the text of the button Label1.Text = "Disconnected" End If Else 'clossing the connection Mycon.con.Close() If Mycon.con.State = ConnectionState.Open Then Button1.Text = "disconnect" 'change the text of the button Label1.Text = "Connected" 'change the text of the label Else Button1.Text = "connect" 'change the text of the button Label1.Text = "Disconnected" 'change the text of the label End If End If End Sub End Class
Posting Komentar