Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.93 KB | None | 0 0
  1. Imports MySql.Data.MySqlClient
  2. Imports System.Security.Cryptography
  3. Public Class Form1
  4. Dim conn As New MySqlConnection
  5. Dim myCommand As New MySqlCommand
  6. Dim myAdapter As New MySqlDataAdapter
  7. Dim myData As New DataTable
  8. Dim SQL As String
  9. Dim SQLRem As String
  10. Dim nome As String
  11. Dim passhash As String
  12. Dim passhashlc As String
  13. Dim Response As MsgBoxResult
  14. Private Function GeraMD5(ByVal texto As String) As String
  15. Dim provider As New MD5CryptoServiceProvider
  16. Dim bytHash() As Byte
  17. Dim hash As String = String.Empty
  18. bytHash = provider.ComputeHash(System.Text.Encoding.UTF8.GetBytes(texto))
  19. provider.Clear()
  20. hash = BitConverter.ToString(bytHash).Replace("-", String.Empty)
  21. Return hash
  22. End Function
  23.  
  24. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  25. Dim conn As MySqlConnection
  26. conn = New MySqlConnection
  27. 'Informações do servidor do Banco de Dados
  28. conn.ConnectionString = "server=localhost;user id=root;password=;database=authdb"
  29. 'Query para selecionar usuário
  30. SQL = "SELECT * FROM authdb_users WHERE username ='" & txtBox.Text & "'"
  31. 'Query para deletar usuário
  32. SQLRem = "DELETE FROM authdb_users WHERE username ='" & txtBox.Text & "' AND password ='" & passhashlc & "'"
  33.  
  34. Try
  35. 'Limpar data para loop
  36. myData.Rows.Clear()
  37. 'Abrir conexão
  38. conn.Open()
  39. 'Gerar hash MD5 da senha
  40. passhash = GeraMD5(TextBox1.Text)
  41. 'Transformar para LowerCase
  42. passhashlc = LCase(passhash)
  43. 'Executar Query MySQL
  44. myCommand.Connection = conn
  45. myCommand.CommandText = SQL
  46. myAdapter.SelectCommand = myCommand
  47. 'Preencher data
  48. myAdapter.Fill(myData)
  49. 'Setar DataGrid para Data
  50. DataGridView1.DataSource = myData
  51. 'Checar se Grid está vazio
  52. If IsNothing(DataGridView1.CurrentRow) Then
  53. 'Caso não encontrado
  54. MsgBox("Usuário não encontrado!")
  55. Else
  56. 'Caso encontrado
  57. MsgBox("Usuário encontrado!")
  58.  
  59. Response = MsgBox("Deseja remover usuário?", MsgBoxStyle.YesNo)
  60. If Response = MsgBoxResult.Yes Then
  61. Try
  62. myCommand.Connection = conn
  63. myCommand.CommandText = SQLRem
  64. myAdapter.SelectCommand = myCommand
  65. myAdapter.Fill(myData)
  66.  
  67. Try
  68. myData.Rows.Clear()
  69. myCommand.Connection = conn
  70. myCommand.CommandText = SQL
  71.  
  72. myAdapter.SelectCommand = myCommand
  73. myAdapter.Fill(myData)
  74. DataGridView1.DataSource = myData
  75. conn.Close()
  76. If IsNothing(DataGridView1.CurrentRow) Then
  77.  
  78. MsgBox("Usuário removido com sucesso!")
  79. Else
  80. MsgBox("Ocorreu algum erro durante a remoção e usuário não foi removido! Contate Administração")
  81. End If
  82.  
  83. Catch ex As MySqlException
  84. MsgBox(ex.Message)
  85. End Try
  86.  
  87.  
  88. Catch ex As MySqlException
  89. MsgBox(ex.Message)
  90. End Try
  91. End If
  92.  
  93.  
  94. End If
  95.  
  96. Catch myerror As MySqlException
  97. MsgBox("There was an error reading from the database: " & myerror.Message)
  98. End Try
  99. conn.Dispose()
  100. conn.Close()
  101.  
  102. End Sub
  103. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement