vb.net使用sqldatareader连接到多个数据库

时间:2017-10-03 10:35:54

标签: sql-server vb.net

      If CbDept_login.Text = "DEPT1" Then
        Dim con As New SqlConnection("Data Source=XYZ-PC;Database=DATABASE1;Integrated Security =true")
        con.Open()
        Dim rs As New SqlCommand("SELECT * FROM TABLE1 WHERE Username=@Username COLLATE SQL_Latin1_General_CP1_CS_AS AND Password=@Password COLLATE SQL_Latin1_General_CP1_CS_AS", con)

        Dim UsernameParam As New SqlParameter("@Username", Me.txtUsername.Text)
        Dim PasswordParam As New SqlParameter("@Password", Me.txtPassword.Text)

        rs.Parameters.Add(UsernameParam)
        rs.Parameters.Add(PasswordParam)


        Dim sqlRead As SqlDataReader = rs.ExecuteReader
        If sqlRead.HasRows Then


            If sqlRead.Read = True Then

                If sqlRead("UserType") = "Admin" Then
                    MsgBox("Log in as " + txtUsername.Text, MsgBoxStyle.OkOnly Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Information, "Login Sucess")
                    frm_Admin.Show()
                    frm_Admin.LblLogAsAdmin_LCR.Text = txtUsername.Text
                    Me.Close()

                ElseIf sqlRead("UserType") = "Staff" Then
                    MsgBox("Log in as " + txtUsername.Text, MsgBoxStyle.OkOnly Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Information, "Login Sucess")
                    frm_User.Show()
                    frm_User.TblogAsuser.Text = txtUsername.Text
                    Me.Close()

                End If

            End If
        Else
            MsgBox("Please input correct username and password", MsgBoxStyle.OkOnly Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical, "Login Failed")

            txtPassword.Text = ""
            con.Close()

        End If
    End If



    If CbDept_login.Text = "DEPT2" Then

        Dim conn As New SqlConnection("Data Source=XYZ-PC;Database=DATABASE2;Integrated Security =true")
        conn.Open()

        Dim rs As New SqlCommand("SELECT * FROM TABLE2 WHERE Username=@Username COLLATE SQL_Latin1_General_CP1_CS_AS AND Password=@Password COLLATE SQL_Latin1_General_CP1_CS_AS", con)

        Dim UsernameParam As New SqlParameter("@Username", Me.txtUsername.Text)
        Dim PasswordParam As New SqlParameter("@Password", Me.txtPassword.Text)

        rs.Parameters.Add(UsernameParam)
        rs.Parameters.Add(PasswordParam)


        Dim sqlRead As SqlDataReader = rs.ExecuteReader
        If sqlRead.HasRows Then


            If sqlRead.Read = True Then

                If sqlRead("UserType") = "Admin" Then
                    MsgBox("Log in as " + txtUsername.Text, MsgBoxStyle.OkOnly Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Information, "Login Sucess")
                    frm_Admin.Show()
                    frm_Admin.LblLogAsAdmin_LCR.Text = txtUsername.Text
                    Me.Close()

                ElseIf sqlRead("UserType") = "Staff" Then
                    MsgBox("Log in as " + txtUsername.Text, MsgBoxStyle.OkOnly Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Information, "Login Sucess")
                    frm_User.Show()
                    frm_User.TblogAsuser.Text = txtUsername.Text
                    Me.Close()

                End If

            End If
        Else
            MsgBox("Please input correct username and password", MsgBoxStyle.OkOnly Or MsgBoxStyle.DefaultButton2 Or MsgBoxStyle.Critical, "Login Failed")

            txtPassword.Text = ""
            conn.Close()

        End If
    End If
End Sub

您好,我目前在我的vb项目中遇到了问题。我使用visual studio 2012和SQL Server 2012作为我的数据库。我有一个登录表单,您可以通过在组合框中选择部门名称来访问不同的数据库,它将检查您是否使用sqldatareader输入正确的用户名和密码取决于部门数据库中的用户名和密码值你选择的。现在我的问题是,当我运行它时,它将显示错误"没有可用的来源"。也许我的代码错了,请帮帮我。先感谢您。顺便说一句,我把这段代码放在我的登录按钮上。

0 个答案:

没有答案