我有三个表users
:departments
和user_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()
但是,它们不工作。
你知道我该如何解决这个问题吗?