C#数据库选择

时间:2018-11-30 03:18:11

标签: c# database listview listbox

我有两个数据库。第一个通过ListBox显示,第二个通过ListView显示。我希望能够根据所选的ListView元素在ListBox中显示一些信息,但是我的代码无法正常工作。

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是某种字符串,但不是。

1 个答案:

答案 0 :(得分:0)

SqlCommand命令=新的SqlCommand($ @“ SELECT * FROM Employee WHERE Dep_nt =” + DpListBox.SelectedValue.ToString(),connection);

请使用参数化存储过程 内联查询是一种糟糕的做法