ExecuteReader 要求已打开且可用的 Connection。连接的当前状态为已关闭。请问大佬知道这个是什么原因吗?
源代码如下:
string name = textBox1.Text.Trim();
string password1 = textBox2.Text.Trim();
string password2 = textBox3.Text.Trim();
string Name = textBox4.Text.Trim();
string number = textBox5.Text.Trim();
if (name == “”)
{
MessageBox.Show(“用户名不能为空!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (password1 != password2)
{
MessageBox.Show(“两次输入的密码不一致!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
SqlConnection con = new SqlConnection();
con.ConnectionString = “Data source=(local);Initial Catalog=Management;Integrated Security=True”;                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select username from manager";
                cmd.Connection = con;
                SqlDataReader reader = cmd.ExecuteReader();
                while(reader.Read())
                {
                    string username = reader["username"].ToString();
                    if (name == username)
                    {
                        MessageBox.Show("用户名已存在!", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        
                    }
                      
                    else
                    {
                        string sql = "insert into manager(username,pwd,name,ID)values(" + name + "," + password1 + "," + Name + "," + number + ")";
                        try
                        {
                            
                            SqlCommand comm = new SqlCommand(sql, con);
                            int count = comm.ExecuteNonQuery();
                            if (count >= 0)
                            {
                                MessageBox.Show("注册成功,请前往登录", "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            }
                            else
                                MessageBox.Show("注册失败", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        finally
                        {
                            con.Close();
                        }
                        Form1 a = new Form1();
                        a.Show();
                        this.Visible=false;                    }
                        
                }            }
        }
--------------------- 
作者:Numb6 
来源:CSDN 
原文:https://blog.csdn.net/weixin_45013749/article/details/90085080 
版权声明:本文为博主原创文章,转载请附上博文链接!

解决方案 »

  1.   

    你网上下个DBHelper类,不要把数据库处理还写在你的逻辑代码实现里面
      

  2.   

     SqlDataReader reader = cmd.ExecuteReader();
    在这行代码之前,加上con.open,只有打卡连接之后,才能read
      

  3.   

    加入了con.open()提示连接已打开,请先关闭原有连接
      

  4.   

    加入con.open(),运行还是报错,提示连接已打开,请先关闭原有的连接。
      

  5.   

    加入了con.open(),运行提示已有连接,请先关闭原有连接
      

  6.   

    在这个SqlDataReader reader = cmd.ExecuteReader();之前加上
    if(con.connectingstate==connetingstate.close)
    {
          con.open();
    }
      

  7.   

    SQLDataReader类型的对象是需要保持连接打开,并且是按顺序读取的,所以你需要加入标识为红色的那行代码才行。
    string name = textBox1.Text.Trim();
     string password1 = textBox2.Text.Trim();
     string password2 = textBox3.Text.Trim();
     string Name = textBox4.Text.Trim();
     string number = textBox5.Text.Trim();
     if (name == “”)
     {
    MessageBox.Show(“用户名不能为空!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     else
     {
     if (password1 != password2)
     {
     MessageBox.Show(“两次输入的密码不一致!”, “登录失败”, MessageBoxButtons.OK, MessageBoxIcon.Information);
     }
     else
     {
     SqlConnection con = new SqlConnection();
    con.ConnectionString = “Data source=(local);Initial Catalog=Management;Integrated Security=True”;                 SqlCommand cmd = new SqlCommand();
                     cmd.CommandText = "select username from manager";
                     cmd.Connection = con;
                     con.open(); 
                    SqlDataReader reader = cmd.ExecuteReader();
                     while(reader.Read())
                     {
                         string username = reader["username"].ToString();
                         if (name == username)
                         {
                             MessageBox.Show("用户名已存在!", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             
                         }
                           
                         else
                         {
                             string sql = "insert into manager(username,pwd,name,ID)values(" + name + "," + password1 + "," + Name + "," + number + ")";
                             try
                             {
                                 
                                 SqlCommand comm = new SqlCommand(sql, con);
                                 int count = comm.ExecuteNonQuery();
                                 if (count >= 0)
                                 {
                                     MessageBox.Show("注册成功,请前往登录", "注册成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                 }
                                 else
                                     MessageBox.Show("注册失败", "注册失败", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             }
                             catch (Exception ex)
                             {
                                 MessageBox.Show(ex.Message, "数据库出错!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                             }
                             finally
                             {
                                 con.Close();
                             }
                             Form1 a = new Form1();
                             a.Show();
                             this.Visible=false;                     }
                             
                     }             }
             }
      

  8.   

    public static MySqlDataReader ExecuteSql(string connectionString, string SQLString, int Times)
            {
                using (MySqlConnection connection = new MySqlConnection(connectionString))
                {
                    using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
                    {
                        try
                        {
                            connection.Open();
                            cmd.CommandTimeout = Times;
                            MySqlDataReader rows = cmd.ExecuteReader();
                            return rows;
                        }
                        catch (MySql.Data.MySqlClient.MySqlException e)
                        {
                            connection.Close();
                            throw e;
                        }
                    }
                }
            }把取数据和逻辑分开
      

  9.   

    con.state <> connection.open 
    con.open
      

  10.   

    首先在实例化Connection后,加上conn.Open();然后你在while循环里边不要Close, 就是finally
                            {
                                con.Close();
                            }删掉;然后在循环外边再Close;