你好
我正在忙于我的学生应用程序,其中一个主题由一个或多个模块组成。
我有一个数据库表,按模块所属的科目对模块进行分组,因此,在添加学生并为某个科目注册时,应将该科目的所有模块添加到他们的个人资料中。
我首先查询数据库以获取特定主题的所有模块的列表
我收到一个错误消息,说已经有一个开放的数据读取器。我认为是因为当我打这条线时"InsertStudyHistory(comboBoxDatabaseConnection,studentModule);",数据读取器rdrSelectedStudentModule仍处于打开状态。我不确定如何解决这个问题
我有用于InsertStudyHistory的代码
任何帮助将不胜感激
我正在忙于我的学生应用程序,其中一个主题由一个或多个模块组成。
我有一个数据库表,按模块所属的科目对模块进行分组,因此,在添加学生并为某个科目注册时,应将该科目的所有模块添加到他们的个人资料中。
我首先查询数据库以获取特定主题的所有模块的列表
C#:
comboBoxDatabaseConnection.Open();
using (SqlCommand cmdInsertStudyHistory = new SqlCommand("select ModuleNumber,ModuleName,ModuleCode from Modules where [email protected] and Archived = '0'", comboBoxDatabaseConnection))
{
cmdInsertStudyHistory.Parameters.AddWithValue("@subjectNumber", studentCourseSubject);
SqlDataReader rdrSelectedStudentModule = cmdInsertStudyHistory.ExecuteReader();
if (rdrSelectedStudentModule.HasRows)
{
while (rdrSelectedStudentModule.Read())
{
studentModule = rdrSelectedStudentModule["ModuleNumber"].ToString();
studentModuleName = rdrSelectedStudentModule["ModuleName"].ToString();
studentModuleCode = rdrSelectedStudentModule["ModuleCode"].ToString();
InsertStudyHistory(comboBoxDatabaseConnection,studentModule);
}
}
rdrSelectedStudentModule.Close();
comboBoxDatabaseConnection.Close();
}
我有用于InsertStudyHistory的代码
C#:
public void InsertStudyHistory(SqlConnection comboBoxDatabaseConnection, string studentModule)
{
using (SqlCommand cmdInsertStudyHistory = new SqlCommand("insert into StudentModule (StudentNumber,CourseNumber,CourseName,SubjectNumber,SubjectName,ModuleNumber,ModuleCode,ModuleName) " +
"VALUES (@StudentNumber,@CourseNumber,@CourseName,@SubjectNumber,@SubjectName,@ModuleNumber,@ModuleCode,@ModuleName)", comboBoxDatabaseConnection))
{
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@StudentNumber", System.Data.SqlDbType.NVarChar, 50, "StudentNumber"));
cmdInsertStudyHistory.Parameters["@StudentNumber"].Value = selectedStudentNumber;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@CourseNumber", System.Data.SqlDbType.NVarChar, 50, "CourseNumber"));
cmdInsertStudyHistory.Parameters["@CourseNumber"].Value = selectedStudentSubject;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@CourseName", System.Data.SqlDbType.NVarChar, 250, "CourseName"));
cmdInsertStudyHistory.Parameters["@CourseName"].Value = editStudentSubjectProgrammeComboBox.Text.ToString();
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@SubjectNumber", System.Data.SqlDbType.NVarChar, 50, "SubjectNumber"));
cmdInsertStudyHistory.Parameters["@SubjectNumber"].Value = editStudentSubjectComboBox.SelectedValue.ToString(); ;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@SubjectName", System.Data.SqlDbType.NVarChar, 250, "SubjectName"));
cmdInsertStudyHistory.Parameters["@SubjectName"].Value = editStudentSubjectComboBox.Text.ToString();
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleNumber", System.Data.SqlDbType.NVarChar, 50, "ModuleNumber"));
cmdInsertStudyHistory.Parameters["@ModuleNumber"].Value = studentModule;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleCode", System.Data.SqlDbType.NVarChar, 50, "ModuleCode"));
cmdInsertStudyHistory.Parameters["@ModuleCode"].Value = studentModuleCode;
cmdInsertStudyHistory.Parameters.Add(new SqlParameter("@ModuleName", System.Data.SqlDbType.NVarChar, 250, "ModuleName"));
cmdInsertStudyHistory.Parameters["@ModuleName"].Value = studentModuleName;
cmdInsertStudyHistory.ExecuteNonQuery();
}
comboBoxDatabaseConnection.Close();
}
}
任何帮助将不胜感激