sql语句应该有两个不同的行,但只返回1行
我有一个sql语句,应该返回2行。第一个是心理id=1,第二个是心理id=2。下面是sql语句sql语句应该有两个不同的行,但只返回1行,sql,arraylist,Sql,Arraylist,我有一个sql语句,应该返回2行。第一个是心理id=1,第二个是心理id=2。下面是sql语句 select * from psychological where patient_id = 12 and symptom = 'delire'; 但是有了这段代码,我用它填充了一个数组列表,它应该是两个不同的行,存在两行,但具有相同的值:第二行 OneSymptomClass oneSymp = new OneSymptomClass(); ArrayList oneSympAll = new A
select * from psychological where patient_id = 12 and symptom = 'delire';
但是有了这段代码,我用它填充了一个数组列表,它应该是两个不同的行,存在两行,但具有相同的值:第二行
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”。有人知道发生了什么吗?您将同一个实例添加到
数组列表中两次。试试这个:
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 oneSympAll=new List();
字符串connstraryList=
“数据源=。\\SQLEXPRESS;”+
“AttachDbFilename=|数据目录| \\PatientMonitoringDatabase.mdf;”+
“初始目录=PatientMonitoringDatabase;”+
“综合安全=真实”;
病人;病人;
字符串查询列表=
“从患者id=处选择*”+
patientID.patient_id+“和症状=”+
症状组合框[tag]。选择EdItem+“';”;
使用(var conn=new SqlConnection(connstarraylist))
{
使用(var cmd=new SqlCommand(queryStrArrayList,conn))
{
conn.Open();
使用(SqlDataReader rdr=cmd.ExecuteReader())
{
while(rdr.Read())
{
OneSympleClass oneSymp=新的OneSympleClass();
心理症状=
转换为32(rdr[“心理id”);
一个症状、病人史、日期=
(DateTime)rdr[“患者病史和日期”];
oneSymp.strength=转换为32(rdr[“强度]);
oneSymp.psy\u开始日期=
(日期时间)rdr[“psy开始日期”];
oneSymp.psy\u结束日期=
(DateTime)rdr[“psy结束日期”];
oneSympAll.Add(OneSymps);
}
}
康涅狄格州关闭();
}
}
MessageBox.Show(oneSympAll[0].psychological_id.ToString());
Show(oneSympAll[1].psychological_id.ToString());
请注意,我将数组列表
替换为列表
。除非您使用.NET 1.1.thx作为提示,否则没有理由使用ArrayList
。我添加了一行,使它工作。这就是你要建议我的吗
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);
}
但是oneSympAll arraylist中不应该有两个不同的oneSymps吗?我的意思是,第一次,一个版本的oneSymp被添加到arraylist中,第二次,一个不同的版本。它们不应该不同吗?@jello:不,你添加了两次相同的引用。