4
我啓用了MARS,但仍然無法在一個連接中執行兩個SqlDataReaders,我搜索了一個解決方案,默認情況下使用RegEdit啓用MARS,但找不到任何解決方案,並且仍然出現該錯誤:已經有一個打開的DataReader與此命令關聯,必須先關閉該命令。MultipleActiveResultSets已啓用但無法正常工作
下面是代碼:
public partial class student_Courses : System.Web.UI.Page
{
string connectionString = "";
string query1 = "";
string query2 = "";
string courseName = "";
string chapterName = "";
string chapterVideoName = "";
string courseValue = "";
SqlDataReader sr2 = null;
protected void Page_Load(object sender, EventArgs e)
{
query1 = "SELECT * FROM Courses";
query2 = "SELECT * FROM Chapters WHERE value='" + courseValue + "'";
connectionString = "Data Source=Prince-PC;" +
"Initial Catalog=Elearning;" +
"Integrated Security=True;"+
"MultipleActiveResultSets=True";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand command1 = new SqlCommand(query1, con);
SqlCommand command2 = new SqlCommand(query2, con);
if (con.State == ConnectionState.Closed)
con.Open();
using (SqlDataReader sr1 = command1.ExecuteReader())
{
while (sr1.Read())
{
courseName = sr1["name"].ToString();
courseValue = sr1["value"].ToString();
sr2 = command2.ExecuteReader();
while (sr2.Read())
{
chapterName = TextBox3.Text = sr2["name"].ToString();
chapterVideoName = Label2.Text = sr2["video_name"].ToString();
}
}
}
con.Close();
}
}