Guest User

Untitled

a guest
Mar 13th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 4.32 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2. Public Class RegSys
  3.     Private _connection As MySqlConnection = New MySqlConnection
  4.     Private _adapter As New MySqlDataAdapter
  5.     Private _query As String
  6.     Private _command As New MySqlCommand
  7.     Private _user As String
  8.     Private _password(2) As String
  9.     Public _continue As Boolean = False
  10.  
  11.     Public Function SetConnectionData(ByVal ip As String, ByVal username As String, ByVal pw As String, ByVal database As String)
  12.         _connection.ConnectionString = "server=" & ip & ";" _
  13.         & "user id=" & username & ";" _
  14.         & "password=" & pw & ";" _
  15.         & "database=" & database
  16.     End Function
  17.  
  18.     Public Function InitializeLogin(ByVal usernametextbox As TextBox, ByVal pwtextbox As TextBox)
  19.         _user = MD5StringHash(usernametextbox.Text)
  20.         _password(0) = MD5StringHash(pwtextbox.Text)
  21.         Try
  22.             _connection.Open()    ' Try to connect
  23.         Catch myerror As MySqlException
  24.             MsgBox("No Connection to the Database!", MsgBoxStyle.Critical, "Fatal Error!")  ' If the person fails to connect
  25.         End Try
  26.         _query = "SELECT * FROM users WHERE username ='" + Replace(_user, " ", "") + "' AND password='" & Replace(_password(0), " ", "") & "'"
  27.         _command.Connection = _connection
  28.         _command.CommandText = _query
  29.  
  30.         _adapter.SelectCommand = _command
  31.         Dim _myData As MySqlDataReader
  32.         _myData = _command.ExecuteReader()    ' Start of the query
  33.         If _myData.HasRows Then  ' If the query contains rows ( the password and username were right )
  34.             MsgBox("Logon sucessful!")
  35.             _connection.Close()
  36.             _continue = True
  37.             TehBot.Show()
  38.  
  39.         Else    ' If username / password are wrong
  40.             MsgBox("Error! Wrong username/password!")
  41.             _continue = False
  42.         End If
  43.     End Function
  44.  
  45.     Public Function InitializeRegister(ByVal usernametextbox As TextBox, ByVal pw1textbox As TextBox, ByVal pw2textbox As TextBox)
  46.         _user = MD5StringHash(usernametextbox.Text)
  47.         _password(0) = MD5StringHash(pw1textbox.Text)
  48.         _password(1) = MD5StringHash(pw2textbox.Text)
  49.         If _password(0) = _password(1) Then  ' If the textboxes passwords are the same
  50.             Try
  51.                 _connection.Open()    ' Open the connection
  52.             Catch myerror As MySqlException
  53.                 MsgBox("No connection to the database!", MsgBoxStyle.Critical, "Fatal Error!")
  54.             End Try
  55.  
  56.             Dim myAdapter As New MySqlDataAdapter
  57.             Dim SQLAbfrage As String = "SELECT * FROM users WHERE username='" + _user + "'" ' Query if the user already exists
  58.             Dim _tmp_myCommand As New MySqlCommand
  59.             _tmp_myCommand.Connection = _connection
  60.             _tmp_myCommand.CommandText = SQLAbfrage
  61.  
  62.             myAdapter.SelectCommand = _tmp_myCommand
  63.             Dim myData As MySqlDataReader
  64.             myData = _tmp_myCommand.ExecuteReader()  ' Start query
  65.  
  66.             If myData.HasRows = 0 Then    ' If the username already exists it won't begin with the registration
  67.                 _connection.Close()
  68.                 _connection.Open()
  69.                 Dim registerfinal As New MySqlDataAdapter
  70.                 _tmp_myCommand.CommandText = "INSERT INTO users(username, password)" _
  71.               & "VALUES('" & _user & "','" & _password(0) & "')"
  72.                 _tmp_myCommand.ExecuteNonQuery()    ' Start SQL query and insert
  73.                 MsgBox("The account with the name: " & usernametextbox.Text & " was created sucessfully!", MsgBoxStyle.Information, "Yep =)")
  74.                 _connection.Close()
  75.             Else
  76.                 MsgBox("This username does already exist", MsgBoxStyle.Information, "Sorry")
  77.             End If
  78.         Else
  79.             MsgBox("The passwords did not match!", MsgBoxStyle.Critical, "Error!")
  80.         End If
  81.     End Function
  82.  
  83.     Private Sub Label2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label2.Click
  84.  
  85.     End Sub
  86.  
  87.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  88.         Dim myRegSys As New RegSys
  89.         myRegSys.SetConnectionData("sql.host.com", "username", "password", "database")
  90.         myRegSys.InitializeRegister(TextBox1, TextBox2, TextBox3)
  91.     End Sub
  92. End Class
Add Comment
Please, Sign In to add comment