Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.23 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2.  
  3. Public Class Connection
  4.     Private connection As MySqlConnection = New MySqlConnection
  5.     Private adapter As New MySqlDataAdapter
  6.     Private query As String
  7.     Private command As New MySqlCommand
  8.     Private user As String
  9.     Private password(2) As String
  10.     Public Whisper As Boolean = False
  11.  
  12.     Public Function SetConnectionData(ByVal ip As String, ByVal username As String, ByVal pw As String, ByVal database As String)
  13.         connection.ConnectionString = "server=" & ip & ";" & "user id=" & username & ";" & "password=" & pw & ";" & "database=" & database
  14.     End Function
  15.  
  16.     Public Function InitializeLogin(ByVal usernametextbox As TextBox, ByVal pwtextbox As TextBox)
  17.         user = MD5StringHash(usernametextbox.Text)
  18.         password(0) = MD5StringHash(pwtextbox.Text)
  19.         Try
  20.             connection.Open()                                                              ' Try to connect
  21.         Catch myerror As MySqlException
  22.             MsgBox("No Connection to the Database!", MsgBoxStyle.Critical, "Fatal Error!")  ' If the person fails to connect
  23.         End Try
  24.         query = "SELECT * FROM Laff WHERE puname ='" + Replace(user, " ", "") + "' AND plass='" & Replace(password(0), " ", "") & "'"
  25.         command.Connection = connection
  26.         command.CommandText = query
  27.  
  28.         adapter.SelectCommand = command
  29.         Dim _myData As MySqlDataReader
  30.         _myData = command.ExecuteReader()                                              ' Start of the query
  31.         If _myData.HasRows Then                                                         ' If the query contains rows ( the password and username were right )
  32.  
  33.             connection.Close()
  34.             Whisper = True
  35.         Else                                                                            ' If username / password are wrong
  36.             MsgBox("Error! Wrong username/password!")
  37.             Whisper = False
  38.         End If
  39.     End Function
  40.  
  41.     Public Function InitializeRegister(ByVal usernametextbox As TextBox, ByVal pw1textbox As TextBox, ByVal pw2textbox As TextBox)
  42.         user = MD5StringHash(usernametextbox.Text)
  43.         password(0) = MD5StringHash(pw1textbox.Text)
  44.         password(1) = MD5StringHash(pw2textbox.Text)
  45.         If password(0) = password(1) Then                                             ' If the textboxes passwords are the same
  46.             Try
  47.                 connection.Open()                                                      ' Open the connection
  48.             Catch myerror As MySqlException
  49.                 MsgBox("No connection to the database!", MsgBoxStyle.Critical, "Fatal Error!")
  50.             End Try
  51.  
  52.             Dim myAdapter As New MySqlDataAdapter
  53.             Dim SQLAbfrage As String = "SELECT * FROM users WHERE username='" + user + "'" ' Query if the user already exists
  54.             Dim _tmp_myCommand As New MySqlCommand
  55.             _tmp_myCommand.Connection = connection
  56.             _tmp_myCommand.CommandText = SQLAbfrage
  57.  
  58.             myAdapter.SelectCommand = _tmp_myCommand
  59.             Dim myData As MySqlDataReader
  60.             myData = _tmp_myCommand.ExecuteReader()                                                 ' Start query
  61.  
  62.             If myData.HasRows = 0 Then                                                              ' If the username already exists it won't begin with the registration
  63.                 connection.Close()
  64.                 connection.Open()
  65.                 Dim registerfinal As New MySqlDataAdapter
  66.                 _tmp_myCommand.CommandText = "INSERT INTO users(username, password)" _
  67.                                      & "VALUES('" & user & "','" & password(0) & "')"
  68.                 _tmp_myCommand.ExecuteNonQuery()                                                    ' Start SQL query and insert
  69.                 MsgBox("The account with the name: " & usernametextbox.Text & " was created sucessfully!", MsgBoxStyle.Information, "Yep =)")
  70.                 connection.Close()
  71.             Else
  72.                 MsgBox("This username does already exist", MsgBoxStyle.Information, "Sorry")
  73.             End If
  74.         Else
  75.             MsgBox("The passwords did not match!", MsgBoxStyle.Critical, "Error!")
  76.         End If
  77.     End Function
  78. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement