当我使用while时读取器关闭时无效尝试调用Read

时间:2015-04-14 16:01:31

标签: threadpool

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();

1 个答案:

答案 0 :(得分:0)

在关闭连接之前,您缺少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();