2014-04-01 20 views
0

我想讓我的用戶按輸入登錄。但是,我有一個數據庫連接與各種驗證。是否有可能將代碼集成到我已有的代碼中。按下數據庫連接點擊輸入

我一直在這個工作幾個小時,現在它讓我發瘋。我已經在沒有數據庫連接的情況下工作,但我需要它來處理現有的代碼。

有人可以告訴我怎麼做到這一點嗎?

這是我已經得到了代碼:

Try 
     Dim objconnection As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Cara\Documents\Visual Studio 2012\Projects\Online Portal Solutions\Online Portal Solutions\Online Portal Solutions Database.mdf;Integrated Security=True") 
     objconnection.Open() 
     Dim SelectStmt As String = "SELECT * FROM [1InnospecLogIn] WHERE Username='" & txt_cusername.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS AND Password='" & txt_cpassword.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS ;" 
     Dim objcommand As SqlCommand = New SqlCommand(SelectStmt, objconnection) 
     Dim reader As SqlDataReader = objcommand.ExecuteReader 

     If reader.Read Then 
      If txt_cpassword.Text <> reader("Password").ToString And txt_cusername.Text <> reader("Username").ToString Then 
       frm_2custhome.Show() 
       Me.Hide() 
       txt_cusername.Text = "" 
       txt_cpassword.Text = "" 
       combocustomer.SelectedIndex = -1 
       txt_cusername.Select() 
      End If 
     Else 
      Static count As Integer = 0 
      Dim prompt As DialogResult = MessageBox.Show("Please enter valid credentials.", "Login Error", 
                 MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning) 
      Select Case prompt 
       Case Windows.Forms.DialogResult.Retry 
        txt_cusername.Text = "" 
        txt_cpassword.Text = "" 
        combocustomer.SelectedIndex = -1 
        txt_cusername.Select() 
        count += 1 
        If count = 3 Then 
         MessageBox.Show("High value of failed login attempts." & vbCrLf & "Application will be terminated for security reasons.", "Error", 
             MessageBoxButtons.OK, MessageBoxIcon.Stop) 
         End 
        End If 
       Case Windows.Forms.DialogResult.Cancel 
        Application.Exit() 
      End Select 
     End If 

     objconnection.Close() 

    Catch ex As Exception 

    End Try 

回答

0

首先你需要改變你的,如果條件:

If txt_cpassword.Text <> reader("Password").ToString And txt_cusername.Text <> reader("Username").ToString Then 

將其更改爲

If txt_cpassword.Text = reader("Password").ToString And txt_cusername.Text = reader("Username").ToString Then 

而且根據我的你的其他部分將進入內部如果condition.Then你的問題如何提供這個按下輸入。首先將現有代碼保存在子例程或函數中,並在需要時調用它。現在,獲取文本框的KeyDown事件並在輸入鍵上調用該函數。看看,

Private Sub txtUserName_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUserName.KeyDown 
    If e.KeyCode = Keys.Enter Then 
     authenticate() ' Function containing that code. 
    End If 
End Sub 

希望它可以幫助你。

+0

您在問題中提供的代碼將該函數保留在函數中。然後轉到表單的設計視圖 - >選擇您希望輸入設備的文本框 - >轉到屬性窗口選擇事件選項卡 - >雙擊KeyDown事件。使用我上面給出的代碼從那裏調用該認證功能。 –