2014-03-26 4 views
0

sqlLink.ConnectionString = "Provider = Microsoft.ACE.OLEDB.12.0; 데이터 원본 = 'C : \ Users \ will \ Documents \ Computing \ ComputingProjectDatabase.accdb';" sqlLink.Open() 시도 txtUsername.Text = ""그럼 있는 MsgBox ("당신은 사용자 이름을 입력하지 않았다!") 돌아 의 ElseIf txtPassword.Text = ""그럼 있는 MsgBox은 ("당신은을 입력하지 않은 경우 암호! ") 돌아 의 ElseIf txtPassword.Text <> txtPasswordConf.Text 그런 있는 MsgBox ("당신의 암호가 일치하지 않습니다! ") 반환 그렇지 VB Access 데이터베이스 중복 항목 방지 오류

   Using sqlOrder As New OleDbCommand 
       Dim sqlCheck As New OleDbCommand 
       sqlCheck.CommandText = "SELECT count(*) FROM LoginSystem WHERE username = @username" 
       sqlCheck.Parameters.AddWithValue("@username", txtUsername.Text) 

       Dim oleRdr As OleDbDataReader 
       oleRdr = sqlCheck.ExecuteReader() 
       If oleRdr.HasRows = True Then 
        oleRdr.Read() 
        If oleRdr.Item(0) = 0 Then 
         sqlOrder.CommandText = "INSERT INTO LoginSystem ([Username], [Password], [PasswordConf]) VALUES ('" & txtUsername.Text & "','" & txtPassword.Text & "','" & txtPasswordConf.Text & "')" 
         sqlOrder.ExecuteNonQuery() 
         MsgBox("You have successfully registered, you can log in now!") 
        Else 
         MsgBox("Username is not currently available, please choose new username.") 
        End If 
       End If 
      End Using 

     End If 

    Catch ex As Exception 
     MsgBox("An error occurred:" & ex.Message) 

    End Try 
    sqlLink.Close() 
    Me.Close() 
    LoginScreen.Show() 
End Sub 

,

안녕하세요.> < 여기에 약간의 질문 만 있습니다. 로그인 데이터 집합의 복사본을 만들 때마다 (이 경우 사용자 이름은 헤더이고 암호는 Trentshop 임) 나는 "데이터가 저장되었습니다."라는 메시지와 함께 반환되며 데이터가 저장되지 않았다는 오류가 표시됩니다. 이미 사용 가능한 복제본이 있습니다.

이상하게도 "사용자 이름은 이미 사용 중입니다. 새 이름을 선택하십시오"라고 말하기를 선호합니다. ExceptDuplicateName 및 다른 유사한 유형으로 주변을 둘러 보았습니다. 그러나 어떤 이유로이 문제에 대해 내 머리를 터지게 할 수는 없습니다. 나는 누군가가이 문제를 해결하도록 도울 수 있다면 큰 도움이 될 것입니다. <

답변

0

이 문제를 해결하는 가장 좋은 방법은 사용자 이름을 사용할 수 있는지 확인하기 전에 확인하는 것입니다. 또한 SQL injection에 대한 도구로 매개 변수화 된 쿼리를 사용해야합니다.

Dim sqlCheck As New OleDbCommand 
sqlCheck.CommandText = "SELECT count(*) FROM LoginSystem WHERE username = @username" 
sqlCheck.Connection = sqlLink 
sqlCheck.Parameters.AddWithValue("@username",txtUsername.Text) 

Dim oleRdr as new OleDbReader = sqlCheck.ExecuteReader() 
If oleRdr.HasRows = True Then 
    oleRdr.Read() 
    If oleRdr.Item(0) = 0 Then 
     'username is available and you can do insert command now 
    Else 
     'username is not available 
    End If 
End If 

또한 올바르게 개체 적절한 처분하기 위해 '사용'문을 사용해야하지만이 질문에 접하는이기 때문에 난 그냥 링크 http://msdn.microsoft.com/en-us/library/htd05whh.aspx

를 제공 할 수 있습니다 또는 당신이를 만들 수 있습니다 스토어드 프로 시저가 성공 또는 실패를 나타내는 값을 반환하는이 전체 프로세스를 처리합니다.

+0

코드에이 코드를 통합했는데 제대로 작동하는 것처럼 보입니다. 그러나 "연결에 오류가 없습니다"가 표시됩니다. 전적으로 그것을 결코 경험하지 못했습니다. 매우 이상합니다. –

+0

판독기 – 5uperdan

+0

을 실행하기 전에 연결 개체에서 .Open() 메서드를 사용해야하고 oledbcommands의 연결 속성을 설정해야합니다. sqlCheck.Connection = sqlLink – 5uperdan