代码丢失(我猜)。我想在列表框中显示学生ID和姓名。但是,我看到这一点:如何打印从SQL Server数据库中检索到的2列
我想不通的问题,尤其是与内部联接。
private void button1_Click(object sender, EventArgs e)
{
string strName = "";
connect.Open();
SqlCommand command = new SqlCommand(" Select Student_tbl.StudentName, Student_tbl.StudentID, Module_tbl.ModuleID FROM[Course-Student] INNER JOIN Student_tbl ON [Course-Student].SID = Student_tbl.StudentID INNER JOIN Module_tbl ON[Course-Student].CID = Module_tbl.ModuleID WHERE(Module_tbl.ModuleID = '" + tbCourse.Text+"')",connect);
command.ExecuteNonQuery();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
strName = reader[1].ToString();
listBox1.Items.Add(strName);
}
connect.Close();
}
不要执行你的查询两次 - 首先用'command.ExecuteNonQuery()'(这里*完全没用*,因为你没有插入或删除任何东西),然后第二次用'.ExecuteReader()'。只调用** **真正需要的是 - 在这里:'ExecuteReader()',因为你要返回你想要迭代的结果集。 –
谢谢..非常有帮助:) –