2016-05-16 76 views
0

當我註冊使用我在VB.NET開發的系統時,我有這個奇怪的問題,它不允許我使用正確的登錄(在登錄表單中)用戶名和密碼我已註冊。但是,當我在Access數據庫中手動輸入用戶名和密碼時,我設法登錄沒有任何問題。這裏是我登錄的代碼和註冊VB.Net 2013(VB未能搜索訪問數據庫)

登錄

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 

請諮詢日Thnx

回答

0

INSERT語句添加空格字符值:

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

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

但是當你從表中選擇,你不要把它們包括:

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

完全刪除空白字符,因爲它們」重新改變你期望這些值的值。

或者更好的是,使用query parameters,因此您不必首先手動創建這些字符串值。 (並且,作爲獎勵,將會關閉您目前擁有的巨大SQL注入漏洞。)

+0

謝謝!快速解決了我的問題。 –