【问题标题】:VB.Net 2013 (VB fail to search the access database)VB.Net 2013(VB搜索access数据库失败)
【发布时间】:2016-05-17 01:18:42
【问题描述】:

当我使用我在 VB.NET 中开发的系统注册时,我遇到了这个奇怪的问题,它不允许我使用我注册的正确用户名和密码登录(在登录表单中)。但是,当我在 Access 数据库中手动输入用户名和密码时,我设法登录没有任何问题。这是我的登录和注册代码

登录

 conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=    C:\Users\lenovo\Documents\Visual Studio 2012\Projects\SDP user interface\SDP user interface\bin\Debug\SCPdatabase.accdb")

  conn.Open()

    sql = "Select * FROM Members WHERE Username ='" & txtusername.Text & "' AND [Password] ='" & txtpassword.Text & " ' "

    cmd = New OleDbCommand(sql, conn)

    dr = cmd.ExecuteReader()

    If dr.HasRows Then
        MessageBox.Show("Login Success")
        Me.Hide()
        Member_Page.Show()
        Member_Page.lblwelcome.Text = "Welcome" & txtusername.Text
    Else
        MessageBox.Show("Login Failed")
    End If

    dr.Close()
    conn.Close()

注册

    Dim flag As Integer

    MyConn.Open()

    sql = "Insert INTO Members (Username,[IC],Email,PhoneNumber,FullName,[Password],Newsletter) values (' " & txtusername3.Text & "','" & txtic3.Text & "','" & txtemail3.Text & "','" & txtphone3.Text & "','" & txtname3.Text & "', ' " & txtpwd3.Text & " ',' " & cmb3.Text & " ')"

    cmd = New OleDbCommand(sql, MyConn)

    flag = cmd.ExecuteNonQuery()

    If flag > 0 Then

        MessageBox.Show(flag & " records added", "Add Records Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If
    MyConn.Close() 'closes the connection

请多多指教

【问题讨论】:

    标签: sql vb.net ms-access


    【解决方案1】:

    您的 INSERT 语句正在向值添加空格字符:

    ' " & txtusername3.Text & "'
     ^--- here
    

    ' " & txtpwd3.Text & " '
     ^--- here            ^--- here
    

    等等

    但是当你从表中选择时,你不包括它们:

    WHERE Username ='" & txtusername.Text & "' AND [Password] ='" & txtpassword.Text & " '
                                                                           just here ---^
    

    完全摆脱空白字符,因为它们会改变您期望的值。

    或者,更好的是,使用query parameters,这样您就不必首先手动构建这些字符串值。 (而且,作为奖励,它会关闭你目前拥有的巨大 SQL 注入漏洞。)

    【讨论】:

    • 谢谢!很快解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多