首页 文章

.executenonquery()error没有给出一个或多个必需参数的值

提问于
浏览
0

Imports System.Data.OleDb

公共类LoginForm Dim connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Desktop\thesis\YBIM.accdb" Dim conn As New OleDbConnection ' TODO: Insert code to perform custom authentication using the provided username and password '(参见http://go.microsoft.com/fwlink/?LinkId=35339) .
' The custom principal can then be attached to the current thread' s的主体如下:' My.User.CurrentPrincipal = CustomPrincipal '其中CustomPrincipal是用于执行身份验证的IPrincipal实现 . ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object ',例如用户名,显示名称等 .

Private Sub LoginForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    conn.ConnectionString = connstring

    If conn.State = ConnectionState.Closed Then
        conn.Open()
        MsgBox("welcome")
    Else
        MsgBox("Cannot connect to database")
    End If
End Sub

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim SqlQuery As String = ("SELECT * FROM tablelogin WHERE Username= @field1 AND Password=@field2")
    Dim SqlCommand As New OleDbCommand
    Dim Sqlrdr As OleDbDataReader

    With SqlCommand
        .CommandText = SqlQuery
        .Connection = conn
        .Parameters.AddWithValue("@field1", UsernameTextBox.Text)
        .Parameters.AddWithValue("@field2", PasswordTextBox.Text)
        .ExecuteNonQuery()
    End With

    Sqlrdr = SqlCommand.ExecuteReader()

    If (Sqlrdr.Read() = True) Then
        home.ShowDialog()
        Me.Hide()
    Else
        MsgBox("wong input")
    End If


End Sub

结束班

1 回答

  • 0

    您的代码中有两个值得注意的事项可以解决 .

    1 *您正在错误地命名参数 . 这个:

    .Parameters.AddWithValue("@field1", UsernameTextBox.Text)
        .Parameters.AddWithValue("@field2", PasswordTextBox.Text)
    

    应该是这样的:

    .Parameters.AddWithValue("field1", UsernameTextBox.Text)
    .Parameters.AddWithValue("field2", PasswordTextBox.Text)
    

    2 *您正在执行命令两次 . 从With语句中删除 .ExecuteNonQuery() ,然后更改:

    Sqlrdr = SqlCommand.ExecuteReader()
    

    Dim ret As Integer
    ret = SqlCommand.ExecuteNonQuery()
    

    而不是使用 Sqlrdr.Read() ,只需检查ret> 0(ExecuteNonQuery返回受命令影响的行数) .

相关问题