0

在此之前,我为 2_1 和 2_2 创建了一个新的“MySqlConnection”和“MySqlCommand”,但这会使我的代码很长而且很混乱,所以我决定重用我已经为 1_1 声明的这些。我已经这样做了:

MySqlConnection connSelect = new MySqlConnection(connectionString);
MySqlCommand cmdSelect = connSelect.CreateCommand();

cmdSelect.Parameters.Add("username", MySqlDbType.VarChar);
cmdSelect.Parameters["username"].Value = Request.QueryString["username"].ToString();

cmdSelect.CommandText = "SELECT * FROM member WHERE username = @username";

connSelect.Open();

MySqlDataReader drSelect1_1 = cmdSelect.ExecuteReader();

if (drSelect1_1.Read())
{
    //1_1
    this.lbl1_1.Text = drSelect1_1["username"].ToString();

//2_1
if (drSelect1_1["direct1"].ToString() != null)
{
    this.lbl2_1.Text = drSelect1_1["direct1"].ToString();

    connSelect.Close();
    cmdSelect.CommandText = "SELECT * FROM member WHERE username = '" + this.lbl2_1.Text + "'";
    connSelect.Open();

    MySqlDataReader drSelect2_1 = cmdSelect.ExecuteReader();

if (drSelect2_1.Read())
{
//some code
}
}

//2_2
if (drSelect1_1["direct2"].ToString() != null)      //ERROR HERE
{
    this.lbl2_2.Text = drSelect1_1["direct2"].ToString();
}
}

2_2 处出现错误:“数据阅读器中没有当前查询”,我在这里做错了吗?请给我建议 thx :)

4

2 回答 2

0

你应该通过像这样遍历阅读器来执行你的“if”条件

while(drSelect1_1.read())
{

// IF CONDITIONS

}

这样你就不会遇到任何空行

于 2012-07-09T07:26:39.567 回答
0

好的,我已经没有主意了,我回到我的原始方法。如果我的时间允许,我会回到这个问题。所以现在大家可以作为参考。如果您有更好的解决方案,请与我们分享。:) 非常感谢。
所以,基本上,我的想法是为每个“IF”条件创建每个 MySqlConnection、MySqlCommand 和 MySqlDataReader。干得好。

  MySqlConnection connSelect = new MySqlConnection(connectionString);
  MySqlCommand cmdSelect = connSelect.CreateCommand();

  cmdSelect.Parameters.Add("username", MySqlDbType.VarChar);
  cmdSelect.Parameters["username"].Value = Request.QueryString["username"].ToString();

  cmdSelect.CommandText = "SELECT * FROM member WHERE username = @username";

  connSelect.Open();

  MySqlDataReader drSelect1_1 = cmdSelect.ExecuteReader();

  if (drSelect1_1.Read())
  {
      //1_1
      this.lbl1_1.Text = drSelect1_1["username"].ToString();   

      //2_1
      if (drSelect1_1["direct1"].ToString() != null)
      {
          this.lbl2_1.Text = drSelect1_1["direct1"].ToString();
          MySqlConnection connSelect2_1 = new MySqlConnection(connectionString);
          MySqlCommand cmdSelect2_1 = connSelect2_1.CreateCommand();

          cmdSelect2_1.CommandText = "SELECT * FROM member WHERE username = '" + this.lbl2_1.Text + "'";

          connSelect2_1.Open();

          MySqlDataReader drSelect2_1 = cmdSelect2_1.ExecuteReader();

          if (drSelect2_1.Read())
          {
              //some code
          }
          connSelect2_1.Close();
      }

      //2_2
      if (drSelect1_1["direct2"].ToString() != null)
      {
          this.lbl2_2.Text = drSelect1_1["direct2"].ToString();

          MySqlConnection connSelect2_2 = new MySqlConnection(connectionString);
          MySqlCommand cmdSelect2_2 = connSelect2_2.CreateCommand();

          cmdSelect2_2.CommandText = "SELECT * FROM member WHERE username = '" + this.lbl2_2.Text + "'";

          connSelect2_2.Open();

          MySqlDataReader drSelect2_2 = cmdSelect2_2.ExecuteReader();

          if (drSelect2_2.Read())
          {
              //some code
          }
          connSelect2_2.Close();          
      }
  }
  connSelect.Close();
于 2012-07-09T08:50:08.497 回答