C# 单击值时显示表的数据-SelectionChanged

C# 单击值时显示表的数据-SelectionChanged,c#,sql,.net,database,wpf,C#,Sql,.net,Database,Wpf,每当我单击EmployeeRank1列表框中的某个名称时,我都希望从EmployeeRank1信息列表框中获取该名称对应表中的相关数据。然而,我得到了一个例外。下面提供了使用的屏幕截图和代码 表格 WPF窗口 例外情况 我使用过的代码 private void employeeRank1List_SelectionChanged(object sender, SelectionChangedEventArgs e) { if(employeeRank1List.Se

每当我单击EmployeeRank1列表框中的某个名称时,我都希望从EmployeeRank1信息列表框中获取该名称对应表中的相关数据。然而,我得到了一个例外。下面提供了使用的屏幕截图和代码

表格

WPF窗口

例外情况

我使用过的代码

private void employeeRank1List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(employeeRank1List.SelectedValue != null)
        {
            // use a try-catch, in case we run into an
            // error while digging into the database
            try
            {
                // create a query and select everything from "EmployeeRank1" table
                // except the Password and GenericPassword columns
                string query = "select * from EmployeeRank1 where Name = @name AND Salary = @salary AND Responsibility = @responsibility AND Position = @position AND Age = @age AND YearsInCompany = @yearsInCompany";

                // Create an SqlCommand and connect to the database
                SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

                // create a connection to the database and run sqlCommand
                SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

                // use the sqlDataAdapter
                using (sqlDataAdapter)
                {
                    // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                    sqlCommand.Parameters.AddWithValue("@name", employeeRank1List.SelectedValue);

                    // create a new data table that allows us
                    // to store data from tables within objects
                    DataTable employeeRank1InformationTable = new DataTable();

                    // fill the sqlDataAdapter with all the
                    // information from the query(from the EmployeeRank1 table)
                    sqlDataAdapter.Fill(employeeRank1InformationTable);

                    // set the content of the employeeRank1List
                    // to be the content from each column in the table
                    employeeRank1Information.DisplayMemberPath = "Salary";
                    employeeRank1Information.DisplayMemberPath = "Position";
                    employeeRank1Information.DisplayMemberPath = "Responsibility";
                    employeeRank1Information.DisplayMemberPath = "Age";
                    employeeRank1Information.DisplayMemberPath = "YearsInCompany";

                    employeeRank1Information.SelectedValuePath = "Id";

                    employeeRank1Information.ItemsSource = employeeRank1InformationTable.DefaultView;
                }
            }
            catch (Exception exception)
            {
                // show what is the error
                MessageBox.Show(exception.ToString());
            }
        }
    }

您已经声明了这些条件的何处没有值

AND Salary = @salary AND
Responsibility = @responsibility AND
Position = @position AND
Age = @age AND
YearsInCompany = @yearsInCompany
您需要在执行查询之前填充它们

sqlCommand.Parameters.AddWithValue("@Salary", <VALUE FROM TABLE>);
但你只填了一个-

sqlCommand.Parameters.AddWithValue("@name", employeeRank1List.SelectedValue);
因此,当单击列表项时,查询基本上与此相同

select * from EmployeeRank1 where Name = <LitsView Item>
AND Salary = UNDEFINED  
AND Responsibility = UNDEFINED 
AND Position = UNDEFINED 
AND Age = UNDEFINED  
AND YearsInCompany = UNDEFINED;
对此

string query = "select * from EmployeeRank1 where Id = @name";
在你尝试这一点后,它会产生不准确的结果。要使其正常工作,请在SQL代码中填充其余的where条件


另外,使用主键/唯一键来定位记录,而不是使用@NAME

您可以将
Id
传递到查询中,因为这是
员工的主键

private void employeerank1列表\u SelectionChanged(对象发送者,selectionchangedventargs e)
{
if(employeeRank1List.SelectedValue==null)//更少的嵌套以翻转条件
回来
尝试
{
//仅选择所需的列
常量字符串查询=@“
选择
身份证件
名称
薪水
位置
责任
年龄
年新公司
来自雇主ANK1 e
其中e.Id=@Id
";
//始终使用新连接并将其弃置
使用(SqlConnection SqlConnection=newsqlconnection(yourConnectionString))
//创建SqlCommand并连接到数据库
使用(SqlCommand-SqlCommand=newsqlcommand(查询,sqlConnection);
{
//始终指定参数类型(以及字符串的长度)
sqlCommand.Parameters.Add(“@id”,SqlDbType.Int).Value=((DataRow)employeeRank1List.SelectedValue)[“id”];
//如何获取Id列??
DataTable Employeerank1信息表=新DataTable();
conn.Open();
使用(SqlDataReader=sqlCommand.ExecuteReader())
{
EmployeeRank1信息表加载(读卡器);
}
}//尽可能快地关闭连接
//只能将一列设置为显示
employeeRank1Information.DisplayMemberPath=“Name”;
EmployeeRank1信息。SelectedValuePath=“Id”;
employeeRank1Information.ItemsSource=employeeRank1Information Table.DefaultView;
}
捕获(异常)
{
//显示错误是什么
MessageBox.Show(exception.Message);
}
}

我通过使用多个列表框而不是一个列表框来实现它。此外,在查询中,我只选择了id,这是主键,然后添加了多个try-catch块以填充我创建的列表框。工作程序的屏幕截图以及代码

节目

代码

// a method that shows information for employees that are rank 1
    private void ShowEmployeeRank1Information()
    {
        // use a try-catch, in case we run into an
        // error while digging into the database
        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1PositionTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1PositionTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1PositionList.DisplayMemberPath = "Position";

                employeeRank1PositionList.SelectedValuePath = "Id";

                employeeRank1PositionList.ItemsSource = employeeRank1PositionTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }
        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1AgeTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1AgeTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1AgeList.DisplayMemberPath = "Age";

                employeeRank1AgeList.SelectedValuePath = "Id";

                employeeRank1AgeList.ItemsSource = employeeRank1AgeTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }
        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1ResponsibilityTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1ResponsibilityTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1ResponsibilityList.DisplayMemberPath = "Responsibility";

                employeeRank1ResponsibilityList.SelectedValuePath = "Id";

                employeeRank1ResponsibilityList.ItemsSource = employeeRank1ResponsibilityTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }

        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1YearsInCompanyTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1YearsInCompanyTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1YearsInCompanyList.DisplayMemberPath = "YearsInCompany";

                employeeRank1YearsInCompanyList.SelectedValuePath = "Id";

                employeeRank1YearsInCompanyList.ItemsSource = employeeRank1YearsInCompanyTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }
    }

    private void employeeRank1List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(employeeRank1List.SelectedValue != null)
        {
            ShowEmployeeRank1Information();
        }
    }

“您需要在执行查询之前将它们归档”是什么意思。我写什么。抱歉,如果我的问题不是最聪明的,但我真的很困惑。我更新了我的答案,因为我不能在这里对它进行评论。在开始这种编码之前,你真的需要了解更多关于SQL和标量变量的知识。谢谢你,这真的很有帮助,我学到了很多。我知道我的SQL知识落后了,但我需要用它做一个项目,而且时间有限。然而,我自己设法解决了这个问题,在下面公布了我的解决方法。你为什么不把
Id
作为一个参数传递呢,我假设它是主键?另外,你应该使用
处理你的连接对象。是的,Id是主键。你能详细说明一下吗关于如何将Id作为参数传递。谢谢,这真的很有帮助,我学到了很多。将Id作为参数传递使其工作。我想出了一种自己的方法,发布在下面。您有相同的代码3次。您可以只返回一个
数据表
,并在所有3个位置引用它。此外,您还需要创建和处理连接离子,不要缓存它。
string query = "select * from EmployeeRank1 where Id = @name";
// a method that shows information for employees that are rank 1
    private void ShowEmployeeRank1Information()
    {
        // use a try-catch, in case we run into an
        // error while digging into the database
        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1PositionTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1PositionTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1PositionList.DisplayMemberPath = "Position";

                employeeRank1PositionList.SelectedValuePath = "Id";

                employeeRank1PositionList.ItemsSource = employeeRank1PositionTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }
        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1AgeTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1AgeTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1AgeList.DisplayMemberPath = "Age";

                employeeRank1AgeList.SelectedValuePath = "Id";

                employeeRank1AgeList.ItemsSource = employeeRank1AgeTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }
        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1ResponsibilityTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1ResponsibilityTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1ResponsibilityList.DisplayMemberPath = "Responsibility";

                employeeRank1ResponsibilityList.SelectedValuePath = "Id";

                employeeRank1ResponsibilityList.ItemsSource = employeeRank1ResponsibilityTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }

        try
        {
            // create a query and select everything from "EmployeeRank1" table
            // except the Password and GenericPassword columns
            string query = "select * from EmployeeRank1 where Id = @id";

            // Create an SqlCommand and connect to the database
            SqlCommand sqlCommand = new SqlCommand(query, sqlConnection);

            // create a connection to the database and run sqlCommand
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);

            // use the sqlDataAdapter
            using (sqlDataAdapter)
            {
                // add a value to the EmployeesRank1 Information table, once an item in the EmployeesRank1 table is clicked
                sqlCommand.Parameters.AddWithValue("@id", employeeRank1List.SelectedValue);

                // create a new data table that allows us
                // to store data from tables within objects
                DataTable employeeRank1YearsInCompanyTable = new DataTable();

                // fill the sqlDataAdapter with all the
                // information from the query(from the EmployeeRank1 table)
                sqlDataAdapter.Fill(employeeRank1YearsInCompanyTable);

                // set the content of the employeeRank1List
                // to be the content from each column in the table
                employeeRank1YearsInCompanyList.DisplayMemberPath = "YearsInCompany";

                employeeRank1YearsInCompanyList.SelectedValuePath = "Id";

                employeeRank1YearsInCompanyList.ItemsSource = employeeRank1YearsInCompanyTable.DefaultView;
            }
        }
        catch (Exception e)
        {
            // show what is the error
            MessageBox.Show(e.ToString());
        }
    }

    private void employeeRank1List_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if(employeeRank1List.SelectedValue != null)
        {
            ShowEmployeeRank1Information();
        }
    }