在此之前我为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:)
答案 0 :(得分:0)
你应该通过像这样的
遍历读者来执行'if'条件while(drSelect1_1.read())
{
// IF CONDITIONS
}
这样你就不会遇到任何空行
答案 1 :(得分:0)
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();