繁体   English   中英

为什么我只能检索最后一行数据而不是所有行? 我正在尝试将行列表填充到ListView中

[英]Why do I only able to retrieve the last row of data instead of all the rows? I'm trying to populate a list of rows into a ListView here

当我已经使用提供的SQL语句完成了整个搜索列表时,它会一直显示最后一行数据

        try
        {
            string strPatients = "SELECT patientID FROM MEDICALHISTORY";
            SqlCommand cmdPatient = new SqlCommand(strPatients, connection);

            string strMedicalPatients = "SELECT pFirstName, pLastName FROM PATIENT WHERE patientID=@searchPatient";
            SqlCommand cmdPatientHistory = new SqlCommand(strMedicalPatients, connection);

            connection.Open();

            SqlDataReader readPatientID = cmdPatient.ExecuteReader();
            if (readPatientID.Read())
            {
                string getID = readPatientID["patientID"].ToString();
                cmdPatientHistory.Parameters.AddWithValue("@searchPatient", getID);
            }
            readPatientID.Close();

            SqlDataReader readPatients = cmdPatientHistory.ExecuteReader();

            while (readPatients.Read())
            {
                ListViewItem allPatients = new ListViewItem(readPatients["pFirstName"].ToString());
                allPatients.SubItems.Add(readPatients["pLastName"].ToString());

                lsMedicalHistory.Items.Add(allPatients);
            }
            readPatients.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error");
        }
        finally
        {
            connection.Close();
        }

知道如何解决这个问题吗? 我整天都在调试它,但仍然找不到运气。 谢谢

问候,泰勒

从我可以从您的代码中得出的结论来看,这是我认为您正在尝试做的事情:

  1. 从MedicalHistory获取所有患者身份
  2. 对于每个这些ID,您都想从患者那里获取详细信息(所以我认为这是1-1关系)

您遇到的问题是,尽管您的第一个查询检索了多个值,但是您只读了第一个查询,然后关闭了连接:

if (readPatientID.Read())
{
    string getID = readPatientID["patientID"].ToString();
    cmdPatientHistory.Parameters.AddWithValue("@searchPatient", getID);
}
readPatientID.Close();

完成此操作后,您仅使用一个ID即可有效地执行第二个命令,该命令仅返回一个结果,这就是为什么您仅看到一个项目的原因。

您可以摆脱第一个查询,然后构造第二个查询,如下所示:

SELECT pFirstName, pLastName FROM PATIENT WHERE patientID in (select patientid from MedicalHistory)

现在,您只需要遍历这些结果。 完整的代码如下所示(在此处键入,因此您可能需要修复可能的错字):

 try
    {
        string strMedicalPatients = "SELECT pFirstName, pLastName FROM PATIENT WHERE patientID in (select patientid from MedicalHistory)";
        SqlCommand cmdPatientHistory = new SqlCommand(strMedicalPatients, connection);

        connection.Open();

        SqlDataReader readPatients = cmdPatientHistory.ExecuteReader();

        while (readPatients.Read())
        {
            ListViewItem allPatients = new ListViewItem(readPatients["pFirstName"].ToString());
            allPatients.SubItems.Add(readPatients["pLastName"].ToString());

            lsMedicalHistory.Items.Add(allPatients);
        }
        readPatients.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error");
    }
    finally
    {
        connection.Close();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM