2015-05-29 104 views
0

我的數據庫:VB.NET管理員和標準用戶登錄失敗

username | password | user_ty 

    roger | 54321 | admin  

    maire | 12345 | user 

我無法登錄到不同的形式。 它只是顯示「不正確的用戶名或密碼」。爲兩個用戶名。

Try 
    Dim con As New SqlConnection("Data Source=192.168.10.3;Initial Catalog=IT_INV;user id=sa;[email protected]") 
    con.Open() 
    Dim rs As New SqlCommand("SELECT * FROM [user] WHERE [email protected] AND [email protected]", con) 

    Dim usernameParam As New SqlParameter("@username", Me.TextBox1.Text) 
    Dim passwordParam As New SqlParameter("@password", Me.TextBox2.Text) 

    rs.Parameters.Add(usernameParam) 
    rs.Parameters.Add(passwordParam) 

    Dim sqlRead As SqlDataReader = rs.ExecuteReader 
    If sqlRead.HasRows Then 
     If sqlRead.Read = True Then 
      If sqlRead("user_ty") = "admin" Then 
       form2.Show() 
       Me.Hide() 

      ElseIf sqlRead("user_ty") = "user" Then 
       form3.Show() 
       Me.Hide() 

      Else 
       MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 

       TextBox1.Text = "" 
       TextBox2.Text = "" 

      End If 
     End If 
    End If 

Catch ex As Exception 
    MessageBox.Show(ex.Message) 
End Try 
+0

[如何問一個問題(https://stackoverflow.com/help/how-to-ask),[如何發佈代碼] (https://stackoverflow.com/help/mcve) – specializt

回答

0

我通常使用這種方法:

Dim cn As New ADODB.Connection() 
    Dim rs As New ADODB.Recordset() 
    Dim sSQL As String 

    Try 

     With cn 
      .ConnectionString = "Data Source=192.168.10.3;Initial Catalog=IT_INV;user id=sa;[email protected]" 
      .Open() 
     End With 
     With rs 
      .CursorLocation = ADODB.CursorLocationEnum.adUseClient 
      .CursorType = ADODB.CursorTypeEnum.adOpenDynamic 
      .LockType = ADODB.LockTypeEnum.adLockOptimistic 
     End With 

     sSQL = String.Format("SELECT * FROM [user] WHERE username='{0}' AND passw='{1}'", Me.TextBox1.Text, Me.TextBox2.Text) 
     rs.Open(sSQL, cn) 

     If Not rs.EOF Then 

      Select Case rs.Fields("user_ty").Value.ToString 
       Case "admin" 
        form2.Show() 
        Me.Hide() 
       Case "user" 
        form3.Show() 
        Me.Hide() 
       Case Else 
        MessageBox.Show("Incorrect Username or Password.", "Login Failed", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 

        TextBox1.Text = "" 
        TextBox2.Text = "" 
      End Select 

     End If 

     rs.Close() 
     cn.Close() 

    Catch ex As Exception 
     MessageBox.Show(ex.ToString) 
    End Try 

    rs = Nothing 
    cn = Nothing