2012-04-25 106 views
4

MARSを有効にしても、1つの接続で2つのSqlDataReaderを実行できない場合は、デフォルトでRegEditを使用してMARSを有効にするソリューションを検索しました。最初に閉じなければならないこのコマンドに関連付けられています。ここで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(); 
    } 
} 

答えて

4

あなたはthis MSDN MARS example

// The following line of code requires 
// a MARS-enabled connection. 
productReader = productCmd.ExecuteReader(); 
using (productReader) 
{ 
    while (productReader.Read()) 
    { 
    Console.WriteLine(" " + 
     productReader["Name"].ToString()); 
    } 
} 
に行われるよう using声明を通じて sr2を処分する必要があります
関連する問題