You are viewing our Forum Archives. To view or take place in current topics click here.
Visual Basic Mysql help...
Posted:
Visual Basic Mysql help...Posted:
Status: Offline
Joined: Feb 11, 201212Year Member
Posts: 2,185
Reputation Power: 88
Status: Offline
Joined: Feb 11, 201212Year Member
Posts: 2,185
Reputation Power: 88
Could I make a program that connects to my mysql database and then (registerd members) of my site can login (on the program) with there username and password? Would this work just by connecting to my database?
#2. Posted:
Status: Offline
Joined: Jun 11, 200915Year Member
Posts: 32
Reputation Power: 2
yes you can and its quite easy to do
hope this helps
any issues give me a shout
Imports MySql.Data.MySqlClient
Public Class RegSys
Private _connection As MySqlConnection = New MySqlConnection
Private _adapter As New MySqlDataAdapter
Private _query As String
Private _command As New MySqlCommand
Private _user As String
Private _password(2) As String
Public _continue As Boolean = False
Public Function SetConnectionData(ByVal ip As String, ByVal username As String, ByVal pw As String, ByVal database As String)
_connection.ConnectionString = "server=" & ip & ";" _
& "user id=" & username & ";" _
& "password=" & pw & ";" _
& "database=" & database
End Function
Public Function InitializeLogin(ByVal usernametextbox As TextBox, ByVal pwtextbox As TextBox)
_user = MD5StringHash(usernametextbox.Text) 'added md5 for abit of security can use you ownr encryption if you want
_password(0) = MD5StringHash(pwtextbox.Text) 'added md5 for abit of security can use your own encryption if you want
_password(0) =
Try
_connection.Open() ' Try to connect
Catch myerror As MySqlException
MsgBox("No Connection To The Database Was Made!", MsgBoxStyle.Critical, "Fatal Error!") ' If the person fails to connect
End Try
_query = "SELECT * FROM users WHERE username ='" + Replace(_user, " ", "") + "' AND password='" & Replace(_password(0), " ", "") & "'"
_command.Connection = _connection
_command.CommandText = _query
' selects the relevant details from the mysql ' database
_adapter.SelectCommand = _command
Dim _myData As MySqlDataReader
_myData = _command.ExecuteReader() ' Start of the query
If _myData.HasRows Then ' If the query contains rows ( the password and username were right )
MsgBox("Logon sucessful!")
_connection.Close()
_continue = True
Else ' If username / password are wrong
MsgBox("Error! Wrong username/password!")
_continue = False
End If
End Function
Public Function InitializeRegister(ByVal usernametextbox As TextBox, ByVal pw1textbox As TextBox, ByVal pw2textbox As TextBox)
_user = MD5StringHash(usernametextbox.Text)
_password(0) = MD5StringHash(pw1textbox.Text)
_password(1) = MD5StringHash(pw2textbox.Text)
If _password(0) = _password(1) Then ' If the textboxes passwords are the same
Try
_connection.Open() ' Open the connection
Catch myerror As MySqlException
MsgBox("No connection to the database!", MsgBoxStyle.Critical, "Fatal Error!")
End Try
Dim myAdapter As New MySqlDataAdapter
Dim SQLAbfrage As String = "SELECT * FROM users WHERE username='" + _user + "'" ' Query if the user already exists
Dim _tmp_myCommand As New MySqlCommand
_tmp_myCommand.Connection = _connection
_tmp_myCommand.CommandText = SQLAbfrage
myAdapter.SelectCommand = _tmp_myCommand
Dim myData As MySqlDataReader
myData = _tmp_myCommand.ExecuteReader() ' Start query
If myData.HasRows = 0 Then ' If the username already exists it won't begin with the registration
_connection.Close()
_connection.Open()
Dim registerfinal As New MySqlDataAdapter
_tmp_myCommand.CommandText = "INSERT INTO users(username, password)" _
& "VALUES('" & _user & "','" & _password(0) & "')"
_tmp_myCommand.ExecuteNonQuery() ' Start SQL query and insert
MsgBox("The account with the name: " & usernametextbox.Text & " was created sucessfully!", MsgBoxStyle.Information, "Yep =)")
_connection.Close()
Else
MsgBox("This username does already exist", MsgBoxStyle.Information, "Sorry")
End If
Else
MsgBox("The passwords did not match!", MsgBoxStyle.Critical, "Error!")
End If
End Function
End Class
Imports System.Security.Cryptography
Imports System.Text
Module MD5SECURITY
Public Function MD5StringHash(ByVal strString As String) As String
Dim MD5 As New MD5CryptoServiceProvider
Dim Data As Byte()
Dim Result As Byte()
Dim Res As String = ""
Dim Tmp As String = ""
Data = Encoding.ASCII.GetBytes(strString)
Result = MD5.ComputeHash(Data)
For i As Integer = 0 To Result.Length - 1
Tmp = Hex(Result(i))
If Len(Tmp) = 1 Then Tmp = "0" & Tmp
Res += Tmp
Next
Return Res
End Function
End Module
Public Function InitializeRegister(ByVal usernametextbox As TextBox, ByVal pw1textbox As TextBox, ByVal pw2textbox As TextBox)
_user = MD5StringHash(usernametextbox.Text)
_password(0) = MD5StringHash(pw1textbox.Text)
_password(1) = MD5StringHash(pw2textbox.Text)
If _password(0) = _password(1) Then ' If the textboxes passwords are the same
Try
_connection.Open() ' Open the connection
Catch myerror As MySqlException
MsgBox("No connection to the database!", MsgBoxStyle.Critical, "Fatal Error!")
End Try
Dim myAdapter As New MySqlDataAdapter
Dim SQLAbfrage As String = "SELECT * FROM users WHERE username='" + _user + "'" ' Query if the user already exists
Dim _tmp_myCommand As New MySqlCommand
_tmp_myCommand.Connection = _connection
_tmp_myCommand.CommandText = SQLAbfrage
myAdapter.SelectCommand = _tmp_myCommand
Dim myData As MySqlDataReader
myData = _tmp_myCommand.ExecuteReader() ' Start query
If myData.HasRows = 0 Then ' If the username already exists it won't begin with the registration
_connection.Close()
_connection.Open()
Dim registerfinal As New MySqlDataAdapter
_tmp_myCommand.CommandText = "INSERT INTO users(username, password)" _
& "VALUES('" & _user & "','" & _password(0) & "')"
_tmp_myCommand.ExecuteNonQuery() ' Start SQL query and insert
MsgBox("The account with the name: " & usernametextbox.Text & " was created sucessfully!", MsgBoxStyle.Information, "Yep =)")
_connection.Close()
Else
MsgBox("This username does already exist", MsgBoxStyle.Information, "Sorry")
End If
Else
MsgBox("The passwords did not match!", MsgBoxStyle.Critical, "Error!")
End If
End Function
End Class
Dim myRegSys As New RegSys
myRegSys.SetConnectionData("localhost/ip", "username", "password", "database")
myRegSys.InitializeRegister(TextBox1, TextBox2, TextBox3)
Dim register As New RegSys
myRegSys.SetConnectionData("localhost/ip", "username", "password", "database")
myRegSys.InitializeRegister(TextBox1, TextBox2, TextBox3)
hope this helps
any issues give me a shout
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.