2016-03-12 158 views
0
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click 

    Dim conn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Jen\Documents\Jade\vb\database.accdb") 

    txtAdmin.Text = "Admin" 

    Dim strsql As New OleDbCommand("select * from Account where Username ='" & txtUsername.Text & "' AND [Password] ='" & txtPassword.Text & "' AND AccountType = '" & txtAdmin.Text & "'", conn) 
    Dim strsql2 As New OleDbCommand("select * from Account where Username ='" & txtUsername.Text & "' AND [Password] ='" & txtPassword.Text & "' AND AccountType = '" & txtStudent.Text & "'", conn) 
    Dim uu As New OleDbParameter("UserName", txtUsername.Text) 
    Dim pp As New OleDbParameter("Password", txtPassword.Text) 
    strsql.Connection.Open() 
    strsql2.Connection.Open() 


    Dim reader As OleDbDataReader 
    reader = strsql.ExecuteReader 

    Dim reader2 As OleDbDataReader 
    reader2 = strsql2.ExecuteReader 

    If reader.HasRows Then 
     strsql.Connection.Close() 

     MsgBox(" Welcome Admin!", vbInformation) 

     frmIndex.Show() 
     desktopFade.Close() 

    ElseIf reader2.HasRows Then 
     strsql2.Connection.Close() 

     MsgBox(" Welcome Student!", vbInformation) 

     frmReg.Show() 
     desktopFade.Close() 

    ElseIf txtUsername.Text = "" And txtPassword.Text = "" Then 
     MsgBox("Don't leave the fields blank", vbCritical) 
     txtUsername.Focus() 

    Else 

     MsgBox("Your Username or Password is invalid", MsgBoxStyle.Critical) 
     Me.txtUsername.Text = "" 
     Me.txtPassword.Text = "" 
     Me.txtUsername.Focus() 
     strsql.Connection.Close() 
     strsql2.Connection.Close() 

    End If 

這裏的錯誤是strsql2.connection.open() < ---它說,連接不緊密。還開着。連接不會關閉(VB)

+0

這可以通過輕鬆解決前開放使用2命令對象的第二個連接對象。重新使用連接對象總是會導致問題。 – Velocibadgery

+0

a)許多這些數據庫提供者對象應該被丟棄b)總是使用SQL參數而不是concat SQL。嘗試使用'D'Artagnan'的用戶名來查看爲什麼c)永遠不會將密碼存儲爲明文,將它們散列,d)從不使用默認表單實例。 – Plutonix

回答

0

我編輯你的問題,因爲你標記它VBA,這是VB.NET

你有幾個問題與您的代碼。 您應該添加錯誤與Try Catch誘捕,也是你的連接不總是關閉

要只解決實際問題,測試如果連接試圖打開它

 If strsql2.Connection.State = ConnectionState.Open Then 
      Console.WriteLine("COnnection already open, closing it") 
      strsql2.Connection.Close() 
     End If 
     strsql2.Connection.Open()