【问题标题】:ExecuteScalar.Connection property has not been initializedExecuteScalar.Connection 属性尚未初始化
【发布时间】:2019-10-01 01:53:46
【问题描述】:
    Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
        If txtUsername.Text = Nothing Or txtPassword.Text = Nothing Then
            MsgBox("Enter Credential", MsgBoxStyle.Exclamation)
        End If
            If connection.State = ConnectionState.Closed Then
            connection.Open()
        End If
        Dim cmd As New OleDbCommand("select count (*) from Admin where username = ? and password = ?, connection")
        cmd.Parameters.AddWithValue("@1", OleDbType.VarChar).Value = txtPassword.Text
        cmd.Parameters.AddWithValue("@2", OleDbType.VarChar).Value = txtUsername.Text
        Dim count = Convert.ToInt32(cmd.ExecuteScalar())
        If (count > 0) Then
            MsgBox("Login Succeed", MsgBoxStyle.Information)
        Else
            MsgBox("Account not found check credentials", MsgBoxStyle.Critical)
        End If
    End Sub
End Class

请任何人帮助我处理此代码... 这是用于登录表单

【问题讨论】:

  • 您将连接作为 SQL 命令字符串的一部分传递...OleDbCommand("select count (*) from Admin where username = ? and password = ?, connection") 应该是 OleDbCommand("select count (*) from Admin where username = ? and password = ?", connection)

标签: .net vb.net winforms


【解决方案1】:

如果您的验证失败,您将需要退出 Sub。 Return 语句完成此操作。

您需要在 Using 块之外声明计数。您将在评估计数后显示消息框,我们不希望在用户响应消息框时连接挂起,因此我们在 Using 块之外进行评估。

将数据库对象保留在本地,以便您可以控制它们何时关闭和处置。 Using...End Using 块即使出现错误也会完成此操作。

如@MathieuGuindon 在他的评论中所说,更正您的OleDbCommand 构造函数。

我很高兴看到正在使用的参数,因此我不愿抱怨,但首选 .Add 方法。 http://www.dbdelta.com/addwithvalue-is-evil/ 和 https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/ 还有一个: https://dba.stackexchange.com/questions/195937/addwithvalue-performance-and-plan-cache-implications 这是另一个 https://andrevdm.blogspot.com/2010/12/parameterised-queriesdont-use.html 实际上,您有点混淆了这两种方法。 .AddWithValue 第二个参数需要的值不是OleDbType。它是 Object 类型,所以它不会产生编译错误,但会在访问数据库时爆炸。

在 .Execute Connections 是宝贵的资源之前不要打开连接,因此您不想让它们在必要时保持打开状态。

最后,永远不要将密码存储为纯文本。我会把它留给你研究用于密码保护的加盐和散列。

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    If String.IsNullOrWhiteSpace(txtUsername.Text) OrElse String.IsNullOrWhiteSpace) txtPassword.Text) Then
        MsgBox("Enter Credential", MsgBoxStyle.Exclamation)
        Return
    End If
    Dim count As Integer
    Using connection As New OleDbConnection("Your connection string")
        Using cmd As New OleDbCommand("select count (*) from Admin where username = ? and password = ?", connection)
            cmd.Parameters.Add("@1", OleDbType.VarChar).Value = txtPassword.Text
            cmd.Parameters.Add("@2", OleDbType.VarChar).Value = txtUsername.Text
            connection.Open()
            count = Convert.ToInt32(cmd.ExecuteScalar())
        End Using
    End Using 'Closes and Disposes the connection
    If count > 0 Then
        MessageBox.Show("Login Succeed", "Login", MessageBoxButtons.OK, MessageBoxIcon.Information)
    Else
        MessageBox.Show("Account not found check credentials", "Login", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End If

End Sub

【讨论】:

  • 玛丽,将旧的 MsgBox 转换为 MessageBox.Show 以完成重写:)
猜你喜欢
  • 2019-12-02
  • 2021-06-21
  • 2017-03-03
  • 2012-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多