C# C数据库选择

C# C数据库选择,c#,database,listview,listbox,C#,Database,Listview,Listbox,我有两个数据库。第一个通过列表框显示,第二个通过列表视图显示。我希望能够基于所选的ListBox元素在ListView中显示一些信息,但我的代码不起作用 private void DpListBox_SelectionChanged(object sender, SelectionChangedEventArgs e) { L.InitReadOnly(false, NameF, LastNameF, AgeF, DepartmentF, ProfessionF, SalaryF);

我有两个数据库。第一个通过列表框显示,第二个通过列表视图显示。我希望能够基于所选的ListBox元素在ListView中显示一些信息,但我的代码不起作用

private void DpListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    L.InitReadOnly(false, NameF, LastNameF, AgeF, DepartmentF, ProfessionF, SalaryF);
    L.InitReadOnly(false, Name4, LastName4, Age4, Department4, Profession4, Salary4);
    int counter = 0;
    IEnumerable<Employee> critCareEmp;
    critCareEmp = L.EmpUpdate(counter, Emp, DpListBox, Ep);

    SqlConnection connection = new SqlConnection(connectionString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    Console.WriteLine(DpListBox.SelectedValue.ToString());
    SqlCommand command = new SqlCommand($@"SELECT * FROM Employee WHERE Dep_nt={DpListBox.SelectedValue}", connection);
    adapter.SelectCommand = command;
    DataTable dataTable1 = new DataTable();
    adapter.Fill(dataTable1);
    Ep.ItemsSource = dataTable1.DefaultView;
}
似乎我需要DpListBox.SelectedValue作为某种字符串,但它不是

SqlCommand command = new SqlCommand
        ($@"SELECT * FROM Employee WHERE Dep_nt="+ 
           DpListBox.SelectedValue.ToString(), 
           connection);
请使用参数化存储过程
内联查询是一种非常糟糕的做法

您到底遇到了哪种错误?如何在DpListBox中加载数据?您正在DpListBox中加载哪些数据?若数据库中Dep_nt列的数据类型为string,则Dep_nt='{DpListBox.SelectedValue}'在sql query.System.Data.SqlClient.SqlException中:无法绑定多部分标识符System.Data.DataRowView。哪一行代码出现错误?DpListBox加载了数据库,其中的数据只是字符串我仍然会收到与以前相同的错误,我想这是因为DpListBox.SelectedValue.ToString=System.Data.DataRowView,它包含两个字段id和DepartmentName,我需要从中选择一个DepartmentName,但我不知道如何在下拉列表中绑定数据问题应该出现在那里