1

我在VB.NET中准备带有 2 个参数的查询时遇到了麻烦。

这是我的代码:

Dim username As String = loginUsername.Value
Dim password As String = EncryptMD5standard(loginPassword.Value)
Dim valid As Boolean = False
Dim connectionString As String = ConfigurationManager.ConnectionStrings("myConnectionString").ConnectionString
        Dim queryString As String = "SELECT id, user_name, role FROM users WHERE user_name = '@user' AND paswd = '@pass'"
        Dim ds As New DataSet()
        Try
            Using connection As New SqlConnection(connectionString)
                Dim command As New SqlCommand(queryString, connection)
                connection.Open()
                command.CommandText = queryString
                command.Parameters.Add("@user", SqlDbType.NVarChar, 15).Value = username
                command.Parameters.Add("@pass", SqlDbType.NVarChar, 32).Value = password
                Dim adapter As New SqlDataAdapter()
                adapter.SelectCommand = command

                adapter.Fill(ds, "login")
                If ds.Tables("login").Rows.Count > 0 Then
                    valid = True
                End If
            End Using
        Catch ex As Exception
            errorLabel.Text = DirectCast(GetLocalResourceObject("erroreDB"), String) & ": " & ex.ToString
        End Try

但是,通过这样做,我的valid值始终为“假”,因此行计数失败。我使用了一些调试,看起来我的表login里面ds是空的。查询有效,我在 SQLServer 替换参数中手动尝试过,但我不明白为什么我的结果为空。

我做错了什么?

4

1 回答 1

3

您不需要将参数用单引号括起来,因为参数系统会处理这一点。

于 2013-02-18T15:18:42.283 回答