2010-03-14 69 views
1

我有一个应该返回2行的sql语句。先用psychological_id = 1,和第二,psychological_id = 2这里是SQL语句sql语句应该有2个不同的行,但只返回1

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”。有人知道发生了什么事?

回答

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()); 

请注意,我用List<OneSymptomClass>取代ArrayList。除非您使用.NET 1.1,否则没有理由使用ArrayList

+0

但不是应该在oneSympAll arraylist中有2个不同的oneSymps吗?我的意思是,第一次,oneSymp的一个版本被添加到数组列表中,第二次,一个不同的版本。他们应该不一样吗? – jello 2010-03-14 04:03:11

+0

@jello:不,你添加了两次相同的参考。 – 2010-03-14 04:11:16

0

thx为提示约翰桑德斯。我添加了一条使其工作的线。那是你要暗示我的吗?

   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); 
       }