asp.net重用连接和命令

时间:2012-07-09 07:21:36

标签: asp.net mysql datareader

在此之前我为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:)

2 个答案:

答案 0 :(得分:0)

你应该通过像这样的

遍历读者来执行'if'条件
while(drSelect1_1.read())
{

// IF CONDITIONS

}

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

答案 1 :(得分: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();