首页 > 解决方案 > VB.NET SQL Server:必须声明标量变量

问题描述

我正在尝试进行从数据库中删除用户的查询。但是当我确认删除用户时,它给了我一个错误:

System.Data.SqlClient.SqlException (0x80131904):必须声明标量变量“@Username”。

Imports System.Data.SqlClient
Public Class DeleteForm
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    Dim RetVal As Integer
    Dim conn = New SqlConnection("Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=dbProject;Integrated Security=True")
    Using cmd = New SqlCommand("select count(*) from tblLogin where username = @Username and password = @Password", conn)
        cmd.Parameters.Add("@Username", SqlDbType.VarChar).Value = txtUsername.Text
        cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = txtPassword.Text

        conn.Open()
        If conn.State = ConnectionState.Open Then
            RetVal = CInt(cmd.ExecuteScalar)
            If RetVal = 1 Then
                If txtPassword.Text And txtCheckPassword.Text <> "" Then
                    If txtCheckPassword.Text = txtPassword.Text Then
                        Dim cancConf As Integer = MessageBox.Show("This cant be undone!" & vbCrLf & "Are you sure?", "Warning!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
                        If cancConf = DialogResult.Yes Then
                            Try
                                Dim queryDelete As String = "DELETE FROM tblLogin WHERE username = @Username"
                                Dim cmdDelete As New SqlClient.SqlCommand(queryCancellazione, conn)

                                cmdCancellazione.ExecuteNonQuery()
                                MsgBox("Account deleted succesfully!")
                                cmdCancellazione.Dispose()
                                conn.Close()

                                LoginForm.Show()
                                Me.Close()
                            Catch ex As Exception
                                MsgBox(ex.ToString())
                            End Try
                        ElseIf cancConf = DialogResult.No Then

                        End If
                    Else
                        MsgBox("The passwords arent matching!", MsgBoxStyle.Exclamation)
                    End If
                ElseIf txtPUtenteCANC.Text <> "" And txtPUtenteCONF.Text = "" Then
                    MsgBox("Please, confirm the password")
                End If
            Else
                MsgBox("User not found!", MsgBoxStyle.Exclamation)
                txtNUtenteCANC.Clear()
                txtPUtenteCANC.Clear()
                txtPUtenteCONF.Clear()
                txtNUtenteCANC.Select()
            End If
        Else
            MessageBox.Show("The connection is not open!" & vbCrLf & "The program will close", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End
        End If
    End Using
End Sub
End Class

标签: sql-servervb.net

解决方案


您已将该参数添加到SELECT COUNT命令中,但未添加到DELETE命令中。

Dim queryCancellazione As String = "DELETE FROM tblLogin WHERE username = @Username"
Dim cmdCancellazione As New SqlClient.SqlCommand(queryCancellazione, conn)
cmdCancellazione.Parameters.Add("@Username", SqlDbType.VarChar).Value = txtUsername.Text

推荐阅读