Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Imports MySql.Data.MySqlClient
- Imports System.Security.Cryptography
- Public Class Form1
- Dim conn As New MySqlConnection
- Dim myCommand As New MySqlCommand
- Dim myAdapter As New MySqlDataAdapter
- Dim myData As New DataTable
- Dim SQL As String
- Dim SQLRem As String
- Dim nome As String
- Dim passhash As String
- Dim passhashlc As String
- Dim Response As MsgBoxResult
- Private Function GeraMD5(ByVal texto As String) As String
- Dim provider As New MD5CryptoServiceProvider
- Dim bytHash() As Byte
- Dim hash As String = String.Empty
- bytHash = provider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(texto))
- provider.Clear()
- hash = BitConverter.ToString(bytHash).Replace("-", String.Empty)
- Return hash
- End Function
- Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
- Dim conn As MySqlConnection
- conn = New MySqlConnection
- 'Informações do servidor do Banco de Dados
- conn.ConnectionString = "server=localhost;user id=root;password=;database=authdb"
- 'Query para selecionar usuário
- SQL = "SELECT * FROM authdb_users WHERE username ='" & txtBox.Text & "'"
- 'Query para deletar usuário
- SQLRem = "DELETE FROM authdb_users WHERE username ='" & txtBox.Text & "' AND password ='" & passhashlc & "'"
- Try
- 'Limpar data para loop
- myData.Rows.Clear()
- 'Abrir conexão
- conn.Open()
- 'Gerar hash MD5 da senha
- passhash = GeraMD5(TextBox1.Text)
- 'Transformar para LowerCase
- passhashlc = LCase(passhash)
- 'Executar Query MySQL
- myCommand.Connection = conn
- myCommand.CommandText = SQL
- myAdapter.SelectCommand = myCommand
- 'Preencher data
- myAdapter.Fill(myData)
- 'Setar DataGrid para Data
- DataGridView1.DataSource = myData
- 'Checar se Grid está vazio
- If IsNothing(DataGridView1.CurrentRow) Then
- 'Caso não encontrado
- MsgBox("Usuário não encontrado!")
- Else
- 'Caso encontrado
- MsgBox("Usuário encontrado!")
- Response = MsgBox("Deseja remover usuário?", MsgBoxStyle.YesNo)
- If Response = MsgBoxResult.Yes Then
- Try
- myCommand.Connection = conn
- myCommand.CommandText = SQLRem
- myAdapter.SelectCommand = myCommand
- myAdapter.Fill(myData)
- Try
- myData.Rows.Clear()
- myCommand.Connection = conn
- myCommand.CommandText = SQL
- myAdapter.SelectCommand = myCommand
- myAdapter.Fill(myData)
- DataGridView1.DataSource = myData
- conn.Close()
- If IsNothing(DataGridView1.CurrentRow) Then
- MsgBox("Usuário removido com sucesso!")
- Else
- MsgBox("Ocorreu algum erro durante a remoção e usuário não foi removido! Contate Administração")
- End If
- Catch ex As MySqlException
- MsgBox(ex.Message)
- End Try
- Catch ex As MySqlException
- MsgBox(ex.Message)
- End Try
- End If
- End If
- Catch myerror As MySqlException
- MsgBox("There was an error reading from the database: " & myerror.Message)
- End Try
- conn.Dispose()
- conn.Close()
- End Sub
- End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement