连接尝试失败,因为连接方在一段时间后没有正确响应 - VB

Posted

技术标签:

【中文标题】连接尝试失败,因为连接方在一段时间后没有正确响应 - VB【英文标题】:A connection attempt failed because the connected party did not properly respond after a period of time - VB 【发布时间】:2020-06-19 12:25:06 【问题描述】:

我正在使用一个 XAMPP 本地托管的 mysql 服务器和我的登录数据库。我正在尝试将数据库中的条目与 vb 程序中文本框的输入进行比较。但是,单击按钮后,程序会冻结。

Imports MySql.Data.MySqlClient
Public Class Login
    Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Parts.User' table. You can move, or remove it, as needed.
        'Me.UserTableAdapter.Fill(Me.Parts.User)

    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Or TextBox2.Text = "" Then
            MsgBox("Oops ¯\_(ツ)_/¯ " + Err.Description(), MsgBoxStyle.OkOnly, "Enter Value")
        Else
            Try
                Dim connectionString = "server=localhost; userid=root; password=; database=partstest1; CharSet=utf8;"
                Dim connDB = New MySqlConnection(connectionString)
                Try
                    connDB.Open()
                    Dim objCmd = New MySqlCommand
                    objCmd.Connection = connDB
                    objCmd.CommandText = "SELECT ID, Username, Password FROM `user` WHERE `Username` = '" + TextBox1.Text + "' and `Password` = '" + TextBox2.Text + "';"
                    objCmd.CommandType = CommandType.Text
                    Dim objAdpt = New MySqlDataAdapter
                    Dim objDS = New DataSet
                    objAdpt.SelectCommand = objCmd
                    objAdpt.Fill(objDS)
                    Console.WriteLine(objDS)
                    If TextBox1.Text = "admin" Then
                        'If Access_Level = 0 Then
                        Me.Hide()
                        AdminMainMenu.Show()
                    Else
                        Me.Hide()
                        MainMenu.Show()
                    End If
                Catch
                    MsgBox("Oops " + Err.Description(), MsgBoxStyle.OkOnly, "Failed to Open")
                    MsgBox("Incorrect login details", MsgBoxStyle.OkOnly)

                End Try
            Catch ex As System.Exception
                System.Windows.Forms.MessageBox.Show(ex.Message)
            End Try
        End If
        TextBox1.Clear()
        TextBox2.Clear()
    End Sub
End Class

我尝试将 TextBox.Text 设置为一个不同的变量,但它没有产生任何影响。我的命令是否太复杂或者我的格式是否错误?我对 SQL 很陌生,因此我们将不胜感激。

【问题讨论】:

从不将密码作为文本存储在您的数据库中,只有散列的密码 哦,它们是散列的,我正在尝试一个未散列的开始,因为我要搞砸的事情更少。我正在使用 SHA256 作为哈希 我也将其简化为简单的单词,看看它是否会对计算时间产生影响 另外,不要在你的 UI 线程上做这个数据库的东西。使用Async / Await 卸载它。这将使您的程序不会冻结,但不一定有助于超时。 对不起,我不太明白,也感谢您编辑我的问题:) 【参考方案1】:

这将在 UI 之外执行数据库操作,并将 UI 元素从数据库操作中移出。它使用Async / Await

Private Async Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If TextBox1.Text = "" Or TextBox2.Text = "" Then
        MsgBox("Oops ¯\_(ツ)_/¯ " + Err.Description(), MsgBoxStyle.OkOnly, "Enter Value")
    Else
        Try
            Await getDataSet(TextBox1.Text, TextBox2.Text)
        Catch ex As Exception
            'MsgBox("Oops " + Err.Description(), MsgBoxStyle.OkOnly, "Failed to Open")
            'MsgBox("Incorrect login details", MsgBoxStyle.OkOnly)
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try
        If TextBox1.Text = "admin" Then
            'If Access_Level = 0 Then
            Me.Hide()
            AdminMainMenu.Show()
        Else
            Me.Hide()
            MainMenu.Show()
        End If
    End If
    TextBox1.Clear()
    TextBox2.Clear()
End Sub

Async Function getDataSet(username As String, password As String) As Task(Of DataSet)
    Return Await Task.Factory.StartNew(
        Function()
            Dim connectionString = "server=localhost; userid=root; password=; database=partstest1; CharSet=utf8;"
            Dim commandText = "SELECT ID, Username, Password FROM `user` WHERE `Username` = '" & username & "' and `Password` = '" & password & "';"
            Using connDB = New MySqlConnection(connectionString), objCmd = New MySqlCommand(), objAdpt = New MySqlDataAdapter()
                connDB.Open()
                objCmd.Connection = connDB
                objCmd.CommandText = commandText 
                objCmd.CommandType = CommandType.Text
                objAdpt.SelectCommand = objCmd
                Dim objDs = New DataSet()
                objAdpt.Fill(objDs)
                Console.WriteLine(objDs)
                Return objDs
            End Using
        End Function)
End Function

我不会解决您潜在的 SQL 注入问题。但如果你愿意,你可以educate yourself。

如果您的数据库连接超时,那么您需要修复您的连接字符串,我猜。我认为没有人能在现场为您提供帮助。

这个答案将帮助您的应用程序不会冻结。

您的应用程序因未关注Separation of Concerns 而受到影响。如果不这样做,您的长时间运行的任务将在 UI 上执行。这里的Async / Await 是一种卸载一些数据库处理的方法。但你也应该明白,应该在 UI 上进行零非 UI 处理。牢记这一点并找到遵守的方法(Async / AwaitBackgroundWorkerSystem.Threading.Thread

【讨论】:

它确实停止了漫长的等待时间。它现在突出显示objAdpt.Fill(objDs) 说我的 sql 语法有问题,我刚刚进入 phpmyadmin 并检查了它在那里工作的查询。感谢您解释 async 和 await 的作用现在很有意义。 @Arc_G 我还将您的一次性对象放入Using 块中,以便在您使用完它们时自动处理它们。这是一个很好的做法。至于查询,破解Using connDB = New ...行的代码,查看commandText的值。有意义吗? 是的,我以前使用过这样的Using connection as New MySql connection("connectionstring") ... End Using,现在检查命令文本值 在我可能有一个全局变量妨碍之前,我删除了它工作的其他地方。 所以可能是其他问题?至少这段代码应该可以帮助您的应用更具响应性

以上是关于连接尝试失败,因为连接方在一段时间后没有正确响应 - VB的主要内容,如果未能解决你的问题,请参考以下文章

连接尝试失败,因为连接方在一段时间后没有正确响应 - VB

SQLSTATE[HY000] [2002] 连接尝试失败,因为连接方在一段时间后没有正确响应,

System.Net.Sockets.SocketException (10060):连接尝试失败,因为连接方在一段时间后没有正确响应

System.Net.Sockets.SocketException

如何从不同的位置向eventhub发布事件?

UWP C#异步套接字服务器不能接受