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