按下Enter键,单击数据库连接

时间:2014-04-01 09:43:11

标签: sql-server database vb.net enter

我试图让我的用户按 Enter 登录。但是,我有一个数据库连接与各种验证。是否有可能将代码集成到我已经拥有的内容中。

我一直在努力工作几个小时,这让我很生气。我没有数据库连接就可以工作,但是我需要它来处理我现有的代码。

有人可以告诉我如何做到这一点吗?

这是我已经获得的代码:

    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

1 个答案:

答案 0 :(得分:0)

首先,您需要更改if条件:

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

根据我的说法,你的其他部分会在内部条件下进入。然后你的问题是如何按 Enter 提供。首先将现有代码保存在子例程或函数中,并在需要时调用它。现在,获取文本框的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

希望它对你有所帮助。