我很难解决我的代码有什么问题,我仅用此错误就花了大约4个小时,我已经尝试关闭所有打开的数据读取器,但仍然收到错误消息“该连接已经有一个打开的DataReader,必须先关闭。”
Try
'///////////////////////
Dim myconn As MySqlConnection = New MySqlConnection("Data Source=localhost;Database=csais;User ID=root;Password=;")
myconn.Open()
Dim query2 = "Select student_id FROM student_subject WHERE student_id = @student_id AND sub_id = @sub_id AND grade = '5' "
cmd = New MySqlCommand(query2, myconn)
cmd.Parameters.AddWithValue("@student_id", StudentID)
cmd.Parameters.AddWithValue("@sub_id", SubjectID)
cmd.Parameters.AddWithValue("@school_year", Sy)
cmd.Parameters.AddWithValue("@semester", Sem)
dr = cmd.ExecuteReader
dr.Read()
If dr.HasRows Then
MessageBox.Show("Subject's failed!")
query2 = "Update student_subject SET school_year = concat(school_year, '" & lbl_As_SchoolYear.Text & "') , enrolled = '1' WHERE student_id = '" & StudentID & "' AND sub_id = '" & SubjectID & "' "
cmd = New MySqlCommand(query2, myconn)
cmd.ExecuteNonQuery()
dr.Close()
End If
Dim query3 = "Select student_id FROM student_subject WHERE student_id = @student_id AND sub_id = @sub_id "
cmd = New MySqlCommand(query3, myconn)
cmd.Parameters.AddWithValue("@student_id", StudentID)
cmd.Parameters.AddWithValue("@sub_id", SubjectID)
cmd.Parameters.AddWithValue("@school_year", Sy)
cmd.Parameters.AddWithValue("@semester", Sem)
dr2 = cmd.ExecuteReader
dr2.Read()
If dr2.HasRows Then
MessageBox.Show("Subject's already taken")
dr2.Close()
Exit Sub
End If
'///////////////////////////
Dim query = "Insert Into student_subject (student_id, sub_Id, grade, enrolled,school_year,semester) Values (@student_id, @sub_id, 0, 1, @school_year, @semester);"
Using cn As New MySqlConnection(connstring)
Using cmd As New MySqlCommand(query, cn)
cmd.Parameters.Add("@student_id", MySqlDbType.Int32).Value = StudentID
cmd.Parameters.Add("@sub_id", MySqlDbType.Int32).Value = SubjectID
cmd.Parameters.AddWithValue("@school_year", Sy)
cmd.Parameters.AddWithValue("@semester", Sem)
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Successful enrollment")
myconn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
MessageBox.Show("Insert Enrollement Function Error to!")
End Try
非常感谢您。