2013-10-21 123 views
0

有人能幫助我我的登錄有問題。 當我嘗試登錄時,出現錯誤「多步OLE DB操作生成錯誤。檢查每個OLE DB狀態值(如果可用),沒有工作完成。」 - 與ExecuteScalar檢索結合。Visual Basic登錄

非常感謝您提供任何幫助,您可能或不可以給我。

Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click 

    If String.IsNullOrWhiteSpace(txtUsername.Text) Then 
     Invalid() 
     Exit Sub 
    End If 

    Using con As New OleDbConnection(connectionString) 
     Dim cmd As New OleDbCommand("Select count(*) From tblAccounts where Username = ?", con) 
     cmd.Parameters.AddWithValue("Username", txtUsername) 
     con.Open() 

     If CType(cmd.ExecuteScalar(), Integer) > 0 Then 
      cmd.CommandText = "Select CPassword From tblAccounts Where Username = ?" 
      Dim matches As String = CType(cmd.ExecuteScalar(), String) 
      If matches = Sha1(txtPassword.Text) Then 
       Response.Redirect("main.aspx") 
      Else 
       Invalid() 
      End If 
     Else 
      Invalid() 
     End If 
    End Using 

End Sub 
+0

從這個鏈接(http://support.microsoft.com/kb/269495/en-us)看來,這錯誤是由您的連接字符串引起的。你可以按照這個鏈接中的建議或發佈'connectionString'內容,以便有人可以幫助你。 PS:你正在執行'cmd.ExecuteScalar()'兩次:在條件語句之外,最好帶上'Dim matches As String = CType(cmd.ExecuteScalar(),String)'行並在條件語句中寫'matches> 0'。 – varocarbas

+0

你打了什麼數據庫? SQL Server/Access/Etc? 「?」參數是數據庫特定的。嘗試在SQL和'cmd.Parameters.AddWithValue(「@用戶名」,txtUsername)'中使用「@UserName」和指定的變量。看看這是否有幫助。 – Steve

回答

0

首先,我會建議(已被建議),你重新檢查連接字符串...

就個人而言,我不會再使用相同的OleDbCommand,因爲這可能導致到不希望的結果。相反,我想嘗試類似如下:

Protected Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click 

    If Not String.IsNullOrWhiteSpace(txtUsername.Text) Then 

     Using con As New OleDbConnection(ConnectionString) 
      con.Open() 

      Dim Result As Integer = 0 

      Using cmd As New OleDbCommand("Select count(*) From tblAccounts where Username = @Username", con) 
       cmd.Parameters.AddWithValue("@Username", txtUsername.text) 

       Result = CInt(cmd.ExecuteScalar) 
      End Using 

      If Result > 0 Then 
       Using cmd As New OleDbCommand("Select CPassword From tblAccounts Where Username = @Username", con) 
        cmd.Parameters.AddWithValue("@Username", txtUsername.text) 

        Dim Obj As Object = cmd.ExecuteScalar() 

        If (Obj IsNot Nothing) AndAlso (Obj IsNot DBNull.Value) Then 
         Dim matches As String = Obj.ToString 

         If matches = SHA1(txtPassword.Text) Then 
          Response.Redirect("main.aspx") 
          Exit Sub 
         End If 

        End If 

       End Using 
      End If 

     End Using 

    End If 

    Invalid() 

End Sub 

希望這有助於