C# c语言中未访问数据库且无输出

C# c语言中未访问数据库且无输出,c#,C#,前两个是类中的函数,第二个是代码。这里有一个文本框,即TXTLICES和combobox cboaccident type,combobox由两个选择项组成,即Death和Major。现在,如果我在TXTLICES中输入licensenumber,并从数据库中的combobox中选择Death选项,它会根据需要显示输出,但问题是,若我在文本框中输入licensenumber,并从数据库中已经存在的combobox中选择major选项,然后按下按钮,那个么它并没有显示major选项的任何输出 pu

前两个是类中的函数,第二个是代码。这里有一个文本框,即TXTLICES和combobox cboaccident type,combobox由两个选择项组成,即Death和Major。现在,如果我在TXTLICES中输入licensenumber,并从数据库中的combobox中选择Death选项,它会根据需要显示输出,但问题是,若我在文本框中输入licensenumber,并从数据库中已经存在的combobox中选择major选项,然后按下按钮,那个么它并没有显示major选项的任何输出

public DataTable Checkdeathaccident(string LicenseNumber, string PhysicalStatus)
{
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
    string sql = "select DeathNumber,ReportNumber,Date from tblAccident  where LicenseNumber=@LicenseNumber and PhysicalStatus=@PhysicalStatus";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber);
    cmd.Parameters.AddWithValue("@PhysicalStatus", PhysicalStatus);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dan = new DataTable();
    da.Fill(dan);
    return dan;
}

public DataTable Checkmajoraccident(string LicenseNumber, string PhysicalStatus)
{
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
    string sql = "select DeathNumber,ReportNumber,Date from tblAccident where LicenseNumber=@LicenseNumber and PhysicalStatus=@PhysicalStatus";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber);
    cmd.Parameters.AddWithValue("@PhysicalStatus", PhysicalStatus);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dl = new DataTable();
    da.Fill(dl);
    return dl;
}    

private void button10_Click_1(object sender, EventArgs e)
{
    DataTable dan = pac.Checkdeathaccident(txtlicense.Text, cboaccidentype.Text);
    if (dan.Rows.Count > 0)
    {
        if (cboaccidentype.Text == "Death")
        {
            dataGridView2.DataSource = dan;

        }
        else
        {
           DataTable dl = pac.Checkmajoraccident(txtlicense.Text, cboaccidentype.Text);
            if (dl.Rows.Count > 0)
            {
                if (cboaccidentype.Text == "Major")
                {

                    dataGridView2.DataSource = dl;

                }                               
            }

        }
    }
    else
    {
        MessageBox.Show("No Record Found");
    }
}

正如Paul提到的,您运行相同的函数来用相同的select查询填充相同的gridview,您可以重构到这一点,它应该可以工作,我没有测试这一点,只是一个想法

public DataTable checkAccident(string LicenseNumber, string PhysicalStatus)
{
    SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB; Integrated Security=True; Initial Catalog=tprojectDB;");
    string sql = "select DeathNumber,ReportNumber,Date from tblAccident where LicenseNumber=@LicenseNumber and PhysicalStatus=@PhysicalStatus";
    SqlCommand cmd = new SqlCommand(sql, con);
    cmd.Parameters.AddWithValue("@LicenseNumber", LicenseNumber);
    cmd.Parameters.AddWithValue("@PhysicalStatus", PhysicalStatus);

    SqlDataAdapter da = new SqlDataAdapter(cmd);

    DataTable dl = new DataTable();
    da.Fill(dl);
    return dl;
}    

private void button10_Click_1(object sender, EventArgs e)
{
    DataTable dan = pac.checkAccident(txtlicense.Text, cboaccidentype.Text);
    if (dan.Rows.Count > 0)
    {
        dataGridView2.DataSource = dan;
    }
    else
    {
        MessageBox.Show("No Record Found");
    }
}
为什么Checkdeathaccident和Checkmajoraccident的功能相同?