簡體   English   中英

有什么辦法可以修復VBNET中的這個錯誤?

[英]Is there any way to fix this error in VBNET?

我正在研究 discord 機器人。

我目前正在使用登錄面板(經典的 MySQL 登錄方式,帶有SELECT語句。)。

但是我已經開始做一個叫做 2FA 的事情,它的工作方式如下:如果登錄成功,應用程序應該將readytotwofactor列(在 MySQL 中)設置為true 如果是真的,discord 機器人會生成一個 10 個字母的代碼,該代碼出現在twofactorcode列中。 BOT 通過查看登錄時在 TextBox 中輸入的用戶名知道將消息發送給誰。它在 MySQL 中查找此信息,並且每一列(注冊用戶名)與一個 discord ID 相關聯。 基於此,BOT 知道將 MySQL 列中檢查的代碼發送給誰,應用程序可以識別代碼是否正確。 發送代碼后, readytotwofactor列將自動更改為false ,我編寫的代碼應將twofactorcode列中的值更改為0

但是,我的問題是它不起作用。

這是源代碼:( Form3是登錄表單, Form4是2FA表單。)。

Imports MySql.Data.MySqlClient

Public Class Form3

    Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        Dim connection As New MySqlConnection("just the login stuff")

        Dim command As New MySqlCommand("SELECT * FROM karolyguilogin WHERE username=@username AND pass=@password", connection)

        command.Parameters.Add("@username", MySqlDbType.VarChar).Value = TextBox1.Text

        command.Parameters.Add("@password", MySqlDbType.VarChar).Value = TextBox2.Text

        Dim command1 As New MySqlCommand("UPDATE `karolyguilogin` readytotwofactor SET readytotwofactor=@readytotwofactor WHERE username=@username", connection)

        Dim Adapter As New MySqlDataAdapter(command)

        Dim table As New DataTable()

        Adapter.Fill(table)

        connection.Open()
        If TextBox1.Text = "" Then
            MessageBox.Show("írj be valamit!")
            Me.Close()
        End If

        If TextBox2.Text = "" Then
            MessageBox.Show("írj be valamit!")
            Me.Close()
        End If




        If table.Rows.Count() <= 0 Then

            MessageBox.Show("Helytelen felhasználónév, vagy jelszó!")


        Else


            command1.Parameters.Add("@readytotwofactor", MySqlDbType.VarChar).Value = "true"


            MessageBox.Show("Hamarosan megkapod a 2FA kódod!")

            Me.Hide()

            Form4.Show()

            Me.Close()

            End If

        connection.Close()

    End Sub
End Class

Imports MySql.Data.MySqlClient

Public Class Form4

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim connection As New MySqlConnection("just the login stuff")

        Dim command As New MySqlCommand("SELECT twofactorcode FROM karolyguilogin WHERE twofactorcode = @twofactorcode <> NULL", connection)

        command.Parameters.Add("@twofactorcode", MySqlDbType.VarChar).Value = TextBox1.Text

        connection.Open()
        If TextBox1.Text = "" Then
            MessageBox.Show("írj be valamit!")
            Me.Close()
        End If

        Dim Adapter As New MySqlDataAdapter(command)

        Dim table As New DataTable()

        Adapter.Fill(table)

        If table.Rows.Count() <= 0 Then

            MessageBox.Show("A kódod helytelen, próbálkozz újra!")
        Else

            Dim command1 As New MySqlCommand("UPDATE `karolyguilogin` twofactorcode SET twofactorcode=@twofactorcode WHERE twofactorcode <> NULL", connection)

            MessageBox.Show("A kódod helyes! Beléphetsz a felületre!")

          command1.Parameters.Add("@twofactorcode", MySqlDbType.VarChar).Value = "NULL"

            Me.Hide()
                Form3.Hide()
                Form1.Show()
                Me.Close()
                Form3.Close()

            End If

        connection.Close()
    End Sub
End Class

首先,密碼永遠不應該以純文本形式存儲。 我希望您為簡潔起見省略了加密代碼。

在 ADO.net 中,需要處理連接和命令,而不僅僅是關閉。 即使出現錯誤,Using 塊也會為您處理此問題。

不要拉下整個記錄。 你只需要知道它是否存在。 您不需要DataAdapter來執行此操作。

在創建任何數據庫對象之前,在 Using 塊之外進行驗證。 另外,當您關閉表單時,您如何期望用戶“輸入內容”?

對第二個查詢使用相同的命令,只需更改CommandText 請注意,我們已經有了@username參數並且@readytotwofactor參數,因為它可以硬編碼在sql 字符串中。

為什么隱藏表單,然后在 2 行后關閉它?

也許您的代碼不起作用,因為您從不執行第二個命令。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    'Validate first before the database code
    If TextBox1.Text = "" OrElse TextBox2.Text = "" Then
        MessageBox.Show("írj be valamit!")
        Exit Sub
    End If
    Dim ReturnCount As Integer
    Using connection As New MySqlConnection("datasource=sql11.freemysqlhosting.net;port=3306;username=sql11396664;password=eG1IbxNzLR;database=sql11396664"),
            command As New MySqlCommand("SELECT Count(*) FROM karolyguilogin WHERE username=@username AND pass=@password", connection)
        command.Parameters.Add("@username", MySqlDbType.VarChar).Value = TextBox1.Text
        command.Parameters.Add("@password", MySqlDbType.VarChar).Value = TextBox2.Text
        connection.Open()
        ReturnCount = CInt(command.ExecuteScalar())
        If ReturnCount = 0 Then
            MessageBox.Show("Helytelen felhasználónév, vagy jelszó!")
        Else
            command.CommandText = "UPDATE `karolyguilogin` readytotwofactor SET readytotwofactor='true' WHERE username=@username"
            command.ExecuteNonQuery()
            MessageBox.Show("Hamarosan megkapod a 2FA kódod!")
            Form4.Show()
            Me.Close()
        End If
    End Using 'Closes and disposes both the command and the connection
End Sub

我假設當readytotwofactor設置為 true 時,數據庫中有某種觸發器。

您意識到您的更新會將karolyguilogin表中的所有記錄設置為字符串“NULL”,而不僅僅是嘗試登錄的用戶。如果這是一個多用戶數據庫,這可能會導致問題。

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If TextBox1.Text = "" Then
        MessageBox.Show("írj be valamit!")
        Exit Sub
    End If
    Dim ReturnCount As Integer
    Using connection As New MySqlConnection("datasource=sql11.freemysqlhosting.net;port=3306;username=sql11396664;password=eG1IbxNzLR;database=sql11396664"),
        command As New MySqlCommand("SELECT Count(*) FROM karolyguilogin WHERE twofactorcode = @twofactorcode", connection)
        command.Parameters.Add("@twofactorcode", MySqlDbType.VarChar).Value = TextBox1.Text
        connection.Open()
        ReturnCount = CInt(command.ExecuteScalar())
        If ReturnCount = 0 Then
            MessageBox.Show("A kódod helytelen, próbálkozz újra!")
        Else
            command.CommandText = "UPDATE `karolyguilogin` SET twofactorcode='NULL' WHERE twofactorcode <> 'NULL';"
            MessageBox.Show("A kódod helyes! Beléphetsz a felületre!")
            Form1.Show()
            Me.Close()
        End If
    End Using 'Closes and disposes both the command and the connection
End Sub

我在翻譯匈牙利語消息框時玩得很開心,這樣我就可以更好地理解您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM