assalammu'alaikum wr.wb.... selamat malam semua....
Pada kesempatan kali ini, saya akan coba berbagi informasi mengenai pembuatan login multiuser pada visual studio 2010 dan MySQL.. Sebelumnya saya sudah membuat sebuah artikel sederhana mengenai pembuatan login dengan visual studio 2010 dan MySQL, namun pada artikel tersebut belum bisa melakukan registrasi untuk penambahan anggota untuk bisa melakukan login.. Maka dari itu malam ini saya akan coba membagikannya
Pertama : Buat database dengan nama userdb dan tabel dengan nama users.. Lalu buat beberapa kolom seperti berikut
Kedua : Buat form desain seperti berikut.. Untuk saya form 1 desain untuk halaman registrasi dan form 2 untuk halaman login.. Seperti berikut :
Note : jangan lupa, karena kita menggunakan database mysql tentu saja kita harus koneksikan mysql.data.. Caranya dengan klik kanan pada project anda lalu pilih add reference dan pilih .NET dan cari mysql.data.. Setelah ketemu kita bisa masukan script..
Ketiga : Masukan script.. Untuk script yang kita masukan terlebih dahulu adalah script registrasi..
Imports MySql.Data.MySqlClient
Public Class Form1
'create a public function and set your MySQL Connection .
Public Function mysqlconnection() As MySqlConnection
'return new connection.
Return New MySqlConnection("server=localhost;user id=root;database=userdb")
End Function
'pass the value of mysqlconnection() as MySQL connection to con.
Public con As MySqlConnection = mysqlconnection()
'declaring the variables string
Public sql As String
Public result As String
'declaring the classes
Dim cmd As New MySqlCommand
Public dt As New DataTable
Public da As New MySqlDataAdapter
Public Sub create(ByVal sql As String)
Try
'open the connection
con.Open()
'set your command
With cmd
'holds the data to be executed
.Connection = con
.CommandText = sql
'execute the data
result = cmd.ExecuteNonQuery
'coditioning the result whether succesfull or failed when it is executed.
If result = 0 Then
'the execution of data is failed
MsgBox("Registration failed!")
Else
'the executed data is succesfull
MsgBox("You are now registered.")
Form2.Show()
Me.Hide()
End If
End With
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'put the string value to sql
sql = "INSERT INTO `users` (`name`, `username`, `Password`, `type` ) VALUES ('" _
& txtname.Text & "','" & txtuser.Text & "','" & txtpass.Text & "','" & cbotype.Text & "')"
'call your sub name and put the sql in the parameters list.
create(sql)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Form2.Show()
Me.Hide()
End Sub
End Class
Selanjutnya adalah script login..
Imports MySql.Data.MySqlClient
Public Class Form2
'create a public function and set your MySQL Connection .
Public Function mysqlconnection() As MySqlConnection
'return new connection.
Return New MySqlConnection("server=localhost;user id=root;database=userdb")
End Function
'pass the value of mysqlconnection() as MySQL connection to con.
Public con As MySqlConnection = mysqlconnection()
'declaring the variables string
Public sql As String
Public result As String
'declaring the classes
Dim cmd As New MySqlCommand
Public dt As New DataTable
Public da As New MySqlDataAdapter
Public Sub loginUser(ByVal sql As String)
Try
'declaring the variable as integer
Dim maxrow As Integer
'open the connection
con.Open()
'every click the button the dt will set a new datatable so that it will retrieve new data in the table
dt = New DataTable
'set a command
With cmd
'holds the data
.Connection = con
.CommandText = sql
End With
'retriving the data
da.SelectCommand = cmd
da.Fill(dt)
'pass the total value of rows in the table to a variable maxrow
maxrow = dt.Rows.Count
'conditioning the total value of rows in the table
'if the total value of rows in the table is greater than 0 then the result is true
' and if not, the result is false
If maxrow > 0 Then
'appearing the record of the current row and the current column in the current table.
MsgBox(dt.Rows(0).Item(1) & " , " & dt.Rows(0).Item(4))
Form3.Show()
Me.Hide()
Else
'the result is false
MsgBox("Account does not exist.")
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
con.Close()
da.Dispose()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'put the string value to sql
sql = "SELECT * from users WHERE username = '" & username.Text & "' and Password = '" & password.Text & "'"
'call your sub name and put the sql in the parameters list.
loginUser(sql)
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Form1.Show()
Me.Hide()
End Sub
End Class
Dan berikut adalah hasilnya. Saya mengatur halaman pertama yang muncul adalah halaman login terlebih dahulu dan saya memilih link "Buat Akun" dan saya buat akun lalu login.
Dan selesai informasi mengenai pembuatan login multiuser dengan menggunakan visual studio 2010 dan Mysql semoga membantu anda.. Great Day !!!
0 komentar:
Posting Komentar