0

我创建了一个登录页面,该页面从我的 SQL 数据库中检索数据以允许用户登录。我还使其区分大小写并添加了一点以在用户输入错误的登录详细信息超过 3 次时终止应用程序。

但是,我为 CompanyA 准备了一张桌子,为 CompanyB 准备了一张桌子;我想从这两个表中选择数据并允许用户登录。

我还希望CompanyA 去Page1,CompanyB 去Page2。

这是从一个表中选择并转到一页的原始代码;我想尝试调整这段代码来回答我的上述问题;有任何想法吗?

    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 [ACustomerLogIn] 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.Clear()
                txt_cpassword.Clear()
            End If
        Else
            Static count As Integer = 0
            Dim prompt As DialogResult = MessageBox.Show("Invaild Username or Password.", "Login Error",
                                                         MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
            Select Case prompt
                Case Windows.Forms.DialogResult.Retry
                    txt_cusername.Text = ""
                    txt_cpassword.Text = ""
                    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
    Catch ex As Exception

    End Try
4

2 回答 2

2

至于一举从两个源表中选择,你可以做这样的事情。使用 UNION,结合两个来源,只提取您真正需要的列(这对于大多数选择查询来说是一个好主意)并使用每个子选择的静态字段来指示它来自哪个表:

SELECT * FROM (select 'A' AS SourceTable, UserID, Username, Password 
    FROM ACustomerLogIn
UNION
SELECT 'B' AS SourceTable, UserID, Username, Password 
    FROM BCustomerLogIn) as CombinedTables
WHERE Username = 'myusername' COLLATE SQL_Latin1_General_CP1_CS_AS
AND Password = 'mypassword' COLLATE SQL_Latin1_General_CP1_CS_AS

使用 'SourceTable' 值(或您选择命名表区分字段的任何名称)来确定下一次将用户发送到哪个表单:

        If txt_cpassword.Text <> reader("Password").ToString And txt_cusername.Text <> reader("Username").ToString Then
            If reader("SourceTable").ToString = "A" then
               frm_2custhome.Show()
            Else
               frm_SomeOtherForm.Show()
            End If
于 2013-10-31T17:35:25.410 回答
-1

我已经修好了。我在下面发布了我的代码,以防它帮助其他人。

    Try
        Dim objconnection1 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")
        objconnection1.Open()
        Dim SelectStmt1 As String = "SELECT * FROM [AJkpLogIn] WHERE Username='" & txt_husername.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS AND Password='" & txt_hpassword.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS ;"
        Dim objcommand1 As SqlCommand = New SqlCommand(SelectStmt1, objconnection1)
        Dim reader1 As SqlDataReader = objcommand1.ExecuteReader

        Dim objconnection2 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")
        objconnection2.Open()
        Dim SelectStmt2 As String = "SELECT * FROM [AClarkeLogIn] WHERE Username='" & txt_husername.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS AND Password='" & txt_hpassword.Text & "' COLLATE SQL_Latin1_General_CP1_CS_AS ;"
        Dim objcommand2 As SqlCommand = New SqlCommand(SelectStmt2, objconnection2)
        Dim reader2 As SqlDataReader = objcommand2.ExecuteReader

        If reader1.Read Then
            If txt_hpassword.Text <> reader1("Password").ToString And txt_husername.Text <> reader1("Username").ToString Then
                frm_Rhauljkphome.Show()
                Me.Hide()
                txt_husername.Clear()
                txt_hpassword.Clear()
            End If

        ElseIf reader2.Read Then
            If txt_hpassword.Text <> reader2("Password").ToString And txt_husername.Text <> reader2("Username").ToString Then
                frm_Shaulclarkehome.Show()
                Me.Hide()
                txt_husername.Clear()
                txt_hpassword.Clear()
            End If

        Else
            Static count1 As Integer = 0
            Dim prompt1 As DialogResult = MessageBox.Show("Invaild Username or Password.", "Login Error",
                                                         MessageBoxButtons.RetryCancel, MessageBoxIcon.Warning)
            Select Case prompt1
                Case Windows.Forms.DialogResult.Retry
                    txt_husername.Text = ""
                    txt_hpassword.Text = ""
                    count1 += 1
                    If count1 = 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
    Catch ex As Exception

    End Try
于 2014-02-03T14:22:25.960 回答