0

我有三个表usersdepartmentsuser_department

我有一个 ASP.NET 页面,“部门”,如下所示:

在此处输入图像描述

当我单击 DropDownList 上的一个项目时,我希望能够显示所有部门成员将显示在 ListBox 中(通过user_department)。

数据库:

在此处输入图像描述

我必须执行什么 SQL 命令?

这是我尝试过的:

SELECT 
    u.username
FROM users u
INNER JOIN user_department ud ON u.user_id = ud.user_id
WHERE ud.department_id = @parameter

VB.NET

 Protected Sub drpDepartments_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drpDepartments.SelectedIndexChanged

        Dim conn As New SqlConnection("Data Source=BRIAN-PC\SQLEXPRESS;Initial Catalog=master_db;Integrated Security=True")
        Dim deptComm As String = "SELECT department_id FROM departments WHERE department_name = @DepartmentName"

        Dim deptSQL As New SqlCommand
        Dim dr As SqlDataReader = deptSQL.ExecuteReader()

        deptSQL = New SqlCommand(deptComm, conn)
        deptSQL.Parameters.AddWithValue("@DepartmentName", drpDepartments.SelectedItem.Text)

        dr.Read()

        If dr.HasRows Then


            Dim department_id As Integer = Convert.ToInt32(dr("department_id"))
            Session("DepartmentID") = department_id

        End If

        dr.Close()
        conn.Close()

但是,它们不工作。

你知道我该如何解决这个问题吗?

4

1 回答 1

1

尝试这个....

SELECT * FROM users u
INNER JOIN 
(
Select x.user_id as userid,x.department_id,y.department_name,y.deparment_code
 from user_Department x 
inner join departments y 
on x.department_id=y.department_id WHERE x.department_id=@yourparameter
) 
    f on u.user_id= f.userid
于 2013-04-13T12:39:58.997 回答