【问题标题】:Invalid attempt to call Read when reader is closed when i use while当我使用 while 关闭阅读器时尝试调用 Read 无效
【发布时间】:2015-06-20 08:15:28
【问题描述】:
con.Open();
    string select = "SELECT * FROM  WHERE username='" + this.textBox1.Text + "' AND pasword='" + this.textBox2.Text + "'";
    SqlCommand cmd = new SqlCommand("SELECT * FROM[Table_2]", con);
    SqlDataReader reader = null;
    reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        if (textBox1.Text == (reader["username"].ToString()) && textBox2.Text == (reader["pasword"].ToString()))
        {
            this.Hide();
            Form2 frm = new Form2();
            frm.Show();
            frm.Activate();
        }
        else
        {
            MessageBox.Show("gabim");
        }
con.Close();

【问题讨论】:

  • 你缺少一些右括号..

标签: threadpool


【解决方案1】:

在关闭连接之前,您在 While 循环中缺少一个右括号。您在阅读时实际上是在关闭连接:

        con.Open();
        string select = "SELECT * FROM  WHERE username='" + this.textBox1.Text + "' AND pasword='" + this.textBox2.Text + "'";
        SqlCommand cmd = new SqlCommand("SELECT * FROM[Table_2]", con);
        SqlDataReader reader = null;
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            if (textBox1.Text == (reader["username"].ToString()) && textBox2.Text == (reader["pasword"].ToString()))
            {
                this.Hide();
                Form2 frm = new Form2();
                frm.Show();
                frm.Activate();
            }
            else
            {
                MessageBox.Show("gabim");
            }
        }

        con.Close();

【讨论】:

    猜你喜欢
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多