sql语句应该有2个不同的行,但只返回1

时间:2010-03-14 03:52:19

标签: sql arraylist

我有一个sql语句应该返回2行。第一个是psychological_id = 1,第二个是psychological_id = 2.这里是sql语句

select * from psychological where patient_id = 12 and symptom = 'delire';

但是使用这段代码,我用一个应该是2个不同行的数组填充数组列表,存在两行但是具有相同的值:第二行。

OneSymptomClass oneSymp = new OneSymptomClass();
ArrayList oneSympAll = new ArrayList();

string connStrArrayList = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
    "Initial Catalog=PatientMonitoringDatabase; " +
    "Integrated Security=True";

string queryStrArrayList = "select * from psychological where patient_id = " + patientID.patient_id + " and symptom = '" + SymptomComboBoxes[tag].SelectedItem + "';";

using (var conn = new SqlConnection(connStrArrayList))
using (var cmd = new SqlCommand(queryStrArrayList, conn))
{
    conn.Open();

    using (SqlDataReader rdr = cmd.ExecuteReader())
    {
        while (rdr.Read())
        {
            oneSymp.psychological_id = Convert.ToInt32(rdr["psychological_id"]);
            oneSymp.patient_history_date_psy = (DateTime)rdr["patient_history_date_psy"];
            oneSymp.strength = Convert.ToInt32(rdr["strength"]);
            oneSymp.psy_start_date = (DateTime)rdr["psy_start_date"];
            oneSymp.psy_end_date = (DateTime)rdr["psy_end_date"];

            oneSympAll.Add(oneSymp);
        }
    }

    conn.Close();
}

OneSymptomClass testSymp = oneSympAll[0] as OneSymptomClass;
MessageBox.Show(testSymp.psychological_id.ToString());

消息框输出“2”,而它应该输出“1”。有人知道发生了什么事吗?

2 个答案:

答案 0 :(得分:0)

您将同一个实例添加到ArrayList两次。试试这个:

List<OneSymptomClass> oneSympAll = new List<OneSymptomClass>();

string connStrArrayList = 
    "Data Source=.\\SQLEXPRESS;" +
    "AttachDbFilename=|DataDirectory|\\PatientMonitoringDatabase.mdf; " +
    "Initial Catalog=PatientMonitoringDatabase; " +
    "Integrated Security=True";

Patient patientID;
string queryStrArrayList =
    "select * from psychological where patient_id = " +
    patientID.patient_id + " and symptom = '" +
    SymptomComboBoxes[tag].SelectedItem + "';";

using (var conn = new SqlConnection(connStrArrayList))
{
    using (var cmd = new SqlCommand(queryStrArrayList, conn))
    {
        conn.Open();

        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            while (rdr.Read())
            {
                OneSymptomClass oneSymp = new OneSymptomClass();
                oneSymp.psychological_id =
                    Convert.ToInt32(rdr["psychological_id"]);
                oneSymp.patient_history_date_psy =
                    (DateTime) rdr["patient_history_date_psy"];
                oneSymp.strength = Convert.ToInt32(rdr["strength"]);
                oneSymp.psy_start_date =
                    (DateTime) rdr["psy_start_date"];
                oneSymp.psy_end_date =
                    (DateTime) rdr["psy_end_date"];

                oneSympAll.Add(oneSymp);
            }
        }

        conn.Close();
    }
}

MessageBox.Show(oneSympAll[0].psychological_id.ToString());
MessageBox.Show(oneSympAll[1].psychological_id.ToString());

请注意,我将ArrayList替换为List<OneSymptomClass>。除非您使用的是.NET 1.1,否则没有理由使用ArrayList

答案 1 :(得分:0)

对于小费约翰桑德斯而言。我添加了一行使其工作。那是你要建议我的吗?

                while (rdr.Read())
                {
                    oneSymp = new OneSymptomClass();

                    oneSymp.psychological_id = Convert.ToInt32(rdr["psychological_id"]);
                    oneSymp.patient_history_date_psy = (DateTime)rdr["patient_history_date_psy"];
                    oneSymp.strength = Convert.ToInt32(rdr["strength"]);
                    oneSymp.psy_start_date = (DateTime)rdr["psy_start_date"];
                    oneSymp.psy_end_date = (DateTime)rdr["psy_end_date"];

                    oneSympAll.Add(oneSymp);
                }